<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code-diesel &#187; files</title>
	<atom:link href="http://www.codediesel.com/tag/files/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Displaying a list of file stats</title>
		<link>http://www.codediesel.com/php/getting-a-quick-list-of-file-stats/</link>
		<comments>http://www.codediesel.com/php/getting-a-quick-list-of-file-stats/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 11:51:32 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[stats]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2683</guid>
		<description><![CDATA[Usually after completing a long project I find that I&#8217;ve created various extraneous files in the project directory; like zip files or maybe a few big graphic files or some huge MySQL dumps. If the directory sizes are small I can manually delete those unwanted files. But if the directory sizes are big or if [...]]]></description>
			<content:encoded><![CDATA[<p>Usually after completing a long project I find that I&#8217;ve created various extraneous files in the project directory; like zip files or maybe a few big graphic files or some huge MySQL dumps. If the directory sizes are small I can manually delete those unwanted files. But if the directory sizes are big or if they are nested deeply, than it can be quite time consuming. Maybe you left a couple of huge MySQL dumps somewhere and forgot to delete them, thus increasing the project file size. And if you are trying to do the cleanup on a online server then it can be even more painful. </p>
<p>Or maybe you are just curious to find how various types of files are taking up your directory space.</p>
<p>Whatever the reason, below is a small php script that displays the distribution of files in a particular directory and its sub-directories by its type. This can be handy if you would like to see which files are taking up space in your project. Although you can easily do such kind of things with a variety of desktop tools, the following code can easily be used online, or integrated into your existing php application.<br />
<span id="more-2683"></span></p>
<h4>Downloading the class</h4>
<p>Before proceeding download the class from below.</p>
<div  class="download2">
<a href="http://www.codediesel.com/downloads/filestats">Download Source</a><br />
<span>Downloads : 361  / File size : 3 kB</span>
</div>
<h4>Displaying file stats</h4>
<p>The class allows you to display the stats in a text format on the command-line or in a HTML format, which you can use online.</p>
<p>To display a text based stats you can use the class as given below and run it from the command line.</p>

<div class="wp_codebox"><table><tr id="p26837"><td class="code" id="p2683code7"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FileTypeStats.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$stats</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_Type_Stats<span style="color: #339933;">;</span>
<span style="color: #000088;">$stats</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/localhost/home/project&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Below is the output for the above. Note that files that do not have an extension are not displayed.</p>

<div class="wp_codebox"><table><tr id="p26838"><td class="code" id="p2683code8"><pre class="xt" style="font-family:monospace;">D:\localhost\test\scan_dir&gt;php textStat.php
&nbsp;
=====================================================
File Type      Total Files      Total Size     %
=====================================================
 php                214           2149 kb    (53.72 %)
 js                  55           1183 kb    (29.57 %)
 db                   6            264 kb    (6.60 %)
 css                 20            144 kb    (3.62 %)
 gif                 90            124 kb    (3.11 %)
 pdf                  2             55 kb    (1.40 %)
 jpg                 48             45 kb    (1.14 %)
 png                 41             24 kb    (0.62 %)
 log                  3              5 kb    (0.13 %)
 sql                  1              3 kb    (0.09 %)
 prefs                1              0 kb    (0.00 %)
 txt                  2              0 kb    (0.00 %)</pre></td></tr></table></div>

<p>Setting a path in the function can be a bit limiting, so you can take the path argument from the command line instead.</p>

<div class="wp_codebox"><table><tr id="p26839"><td class="code" id="p2683code9"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FileTypeStats.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$stats</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_Type_Stats<span style="color: #339933;">;</span>
<span style="color: #000088;">$stat_data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$stats</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now you can get the path from the command line.</p>

<div class="wp_codebox"><table><tr id="p268310"><td class="code" id="p2683code10"><pre class="text" style="font-family:monospace;">D:\localhost\test\scan_dir&gt;php textStat.php /localhost/home/project/</pre></td></tr></table></div>

<p>Set the <em>textStat.php</em> in you path variable, and you will have the file stat functionality available anywhere.</p>
<p>Someone could have written this class as a shell script easily, but the advantage of doing it in PHP is that we can also have a HTML version of the above stat display, as shown below.</p>
<p>To display HTML table based stats you can use the class with the <em>getHtml()</em> method and run it from the browser.</p>

<div class="wp_codebox"><table><tr id="p268311"><td class="code" id="p2683code11"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;style.css&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FileTypeStats.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$stats</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_Type_Stats<span style="color: #339933;">;</span>
<span style="color: #000088;">$stat_data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$stats</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHtml</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/localhost/home/project&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$stat_data</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Below is the HTML output.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2010/08/file_stat.png"><img src="http://www.codediesel.com/wp-content/uploads/2010/08/file_stat.png" alt="" title="file_stat" width="400" height="414" class="center size-full wp-image-2685" /></a></p>
<p>The style is defined in a CSS, so you can customize it to your liking.</p>
<p>A word of caution. For hugely nested directory running into thousands of files the script can timeout, so make sure that you increase the php <em>max_execution_time</em> in your script to an appropriate number.</p>

<div class="wp_codebox"><table><tr id="p268312"><td class="code" id="p2683code12"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/getting-a-quick-list-of-file-stats/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Search &amp; replace in files using php</title>
		<link>http://www.codediesel.com/php/search-replace-in-files-using-php/</link>
		<comments>http://www.codediesel.com/php/search-replace-in-files-using-php/#comments</comments>
		<pubDate>Mon, 11 May 2009 13:57:51 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=819</guid>
		<description><![CDATA[Searching and replacing content in files is a common task all of us do regularly. Most programmers will implement it using Perl a shell script or through a editor. Perl offers itself as an excellent tool for the required purpose; we PHP programmers are not quite so lucky in that matter. Search/replace is easier from [...]]]></description>
			<content:encoded><![CDATA[<p>Searching and replacing content in files is a common task all of us do regularly. Most programmers will implement it using Perl a shell script or through a editor. Perl offers itself as an excellent tool for the required purpose; we PHP programmers are not quite so lucky in that matter. Search/replace is easier from a shell prompt or an editor, but what if you have to do the same programatically in php. <a target="_blank" href="http://pear.php.net/package/File_SearchReplace/download">File_SearchReplace</a> is a pear package that helps you search/replace in files through a nice object oriented interface.<br />
<span id="more-819"></span><br />
<strong>Installation</strong><br />
Pear installation as usual is simple.</p>

<div class="wp_codebox"><table><tr id="p81919"><td class="code" id="p819code19"><pre class="dos" style="font-family:monospace;">c:/&gt; pear install File_SearchReplace</pre></td></tr></table></div>

<p><strong>Doing a simple search &#038; replace</strong><br />
The following is an simple example code that searches the file &#8216;fruits.txt&#8217; and replaces all occurrences of &#8216;apples&#8217; with &#8216;oranges&#8217;. The <em>getNumOccurences</em> function returns the total number of replaced strings in the file.</p>

<div class="wp_codebox"><table><tr id="p81920"><td class="code" id="p819code20"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fruits.txt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;apples&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;oranges&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #009933; font-style: italic;">// directorie(s) to search</span>
                              <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The fourth option in the <em>File_SearchReplace</em> specifies a optional directory name to search. If the directory name is empty than the files will be searched in the current directory or in the respective path if a path is also included with the filename. Following is an example if you want to search all files in the directory &#8216;nature/fruits&#8217;.</p>

<div class="wp_codebox"><table><tr id="p81921"><td class="code" id="p819code21"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nature/fruits/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;apples&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;oranges&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span> <span style="color: #009933; font-style: italic;">// directory to search</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// 'true' to search subdirectories</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>All the four starting options of <em>File_SearchReplace</em> are of mixed type; i.e they take a string or an array of strings as their options. For example in the following all occurrences of &#8216;apples&#8217; will be replaced by &#8216;oranges&#8217; and that of &#8216;pears&#8217; by &#8216;grapes&#8217;.</p>

<div class="wp_codebox"><table><tr id="p81922"><td class="code" id="p819code22"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nature/fruits/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'apples'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pears'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'oranges'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'grapes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You don&#8217;t have to create a new instance everytime you need a new search, you can set the various parameters through the interface provided by the <em>File_SearchReplace</em> class as shown below.</p>

<div class="wp_codebox"><table><tr id="p81923"><td class="code" id="p819code23"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nature/fruits/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'apples'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pears'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'oranges'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'grapes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Start a new search */</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFind</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;oranges&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setReplace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;berries&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Regular Expression search</strong><br />
You can also use a regular expression in a search string, but before that we must specify what kind of search is required with the &#8216;setSearchFunction&#8217; as shown below. The following example replaces all occurrences of &#8216;color&#8217; or &#8216;colour&#8217; with the capital &#8216;COLOR&#8217;.</p>

<div class="wp_codebox"><table><tr id="p81924"><td class="code" id="p819code24"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test/graphics/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/col(o|ou)r/'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'COLOR'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSearchFunction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;preg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The <em>setSearchFunction</em> takes one of the following four options:</p>
<p>normal &#8211; default<br />
quick &#8211; use str_replace()<br />
preg &#8211; use preg_replace()<br />
ereg &#8211; use ereg_replace()</p>
<p>More information on this options can be found <a target="_blank" href="http://pear.php.net/manual/en/package.filesystem.file-searchreplace.intro.php">here</a>.</p>
<p><strong>In conclusion</strong><br />
The package can be quite useful when you want to replace large quantities of text programatically. As it works on plain strings and regular expressions, its can be quite a handy tool in many occasions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/search-replace-in-files-using-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

