<?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>Beaver6813.com &#187; replace</title>
	<atom:link href="http://beaver6813.com/tag/replace/feed/" rel="self" type="application/rss+xml" />
	<link>http://beaver6813.com</link>
	<description>Web Developer Extraordinaire!</description>
	<lastBuildDate>Thu, 05 Jan 2012 20:33:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-19672</generator>
		<item>
		<title>PHP Search and Replace Directory Recursively</title>
		<link>http://beaver6813.com/2010/04/php-search-and-replace-directory-recursively/</link>
		<comments>http://beaver6813.com/2010/04/php-search-and-replace-directory-recursively/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 16:04:11 +0000</pubDate>
		<dc:creator>Beaver6813</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[find and replace]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[recurse]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[search and replace]]></category>

		<guid isPermaLink="false">http://beaver6813.com/?p=207</guid>
		<description><![CDATA[On a message forum I regularily visit a member was having issues with code being placed in multiple files that he wanted to remove or change without having to edit each and every file manually. For this I created a &#8230; <a href="http://beaver6813.com/2010/04/php-search-and-replace-directory-recursively/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://beaver6813.com/2009/10/bash-bad-interpreter/' rel='bookmark' title='Bash, Bad Interpreter'>Bash, Bad Interpreter</a></li>
<li><a href='http://beaver6813.com/2010/03/godaddy-mod_rewrite-hell/' rel='bookmark' title='GoDaddy mod_rewrite Hell'>GoDaddy mod_rewrite Hell</a></li>
<li><a href='http://beaver6813.com/2009/10/fetchmp3-com/' rel='bookmark' title='FetchMP3.com'>FetchMP3.com</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>On a message forum I regularily visit a member was having issues with code being placed in multiple files that he wanted to remove or change without having to edit each and every file manually. For this I created a search and replace function which will recursively search the directory and every file<span id="more-207"></span> for a specific piece of code and will replace with whatever:</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p207code2'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2072"><td class="code" id="p207code2"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span>?php
<span style="color: #000088;">$stringsearch</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;texttosearchfor&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stringreplace</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;texttoreplacewith&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;./&quot;</span><span style="color: #339933;">;</span>
SearchandReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #339933;">,</span><span style="color: #000088;">$stringsearch</span><span style="color: #339933;">,</span><span style="color: #000088;">$stringreplace</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> SearchandReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stringsearch</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stringreplace</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Starting search for <span style="color: #006699; font-weight: bold;">$stringsearch</span> within directory <span style="color: #006699; font-weight: bold;">$dir</span>
&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$listDir</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handler</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/opendir"><span style="color: #990000;">opendir</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sub</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/readdir"><span style="color: #990000;">readdir</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handler</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sub</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;.&quot;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$sub</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;..&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$sub</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;Thumb.db&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_file"><span style="color: #990000;">is_file</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$sub</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/substr_count"><span style="color: #990000;">substr_count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sub</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                            <span style="color: #009900;">&#123;</span>
                            <span style="color: #000088;">$getfilecontents</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/file_get_contents"><span style="color: #990000;">file_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$sub</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/substr_count"><span style="color: #990000;">substr_count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$getfilecontents</span><span style="color: #339933;">,</span><span style="color: #000088;">$stringsearch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
                            <span style="color: #009900;">&#123;</span>
                            <span style="color: #000088;">$replacer</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stringsearch</span><span style="color: #339933;">,</span><span style="color: #000088;">$stringreplace</span><span style="color: #339933;">,</span><span style="color: #000088;">$getfilecontents</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #666666; font-style: italic;">// Let's make sure the file exists and is writable first.</span>
                              <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_writable"><span style="color: #990000;">is_writable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$sub</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$sub</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                       <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Cannot open file (&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$sub</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
                                       <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span>
                                  <span style="color: #009900;">&#125;</span>
                                  <span style="color: #666666; font-style: italic;">// Write $somecontent to our opened file.</span>
                                  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/fwrite"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$replacer</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Cannot write to file (&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$sub</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
                                      <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span>
                                  <span style="color: #009900;">&#125;</span>
                                  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Success, removed searched content from:&quot;</span><span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$sub</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;
&quot;</span><span style="color: #339933;">;</span>
                                  <a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                              <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                                  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;The file &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$sub</span><span style="color: #0000ff;">&quot; is not writable
&quot;</span><span style="color: #339933;">;</span>
                              <span style="color: #009900;">&#125;</span>
                            <span style="color: #009900;">&#125;</span>
                            <span style="color: #009900;">&#125;</span>
                        <span style="color: #000088;">$listDir</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sub</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_dir"><span style="color: #990000;">is_dir</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$sub</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$listDir</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$sub</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> SearchandReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$sub</span><span style="color: #339933;">,</span><span style="color: #000088;">$stringsearch</span><span style="color: #339933;">,</span><span style="color: #000088;">$stringreplace</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <a href="http://www.php.net/closedir"><span style="color: #990000;">closedir</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handler</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$listDir</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
?<span style="color: #339933;">--&gt;</span></pre></td></tr></table></div>

<p>Related posts:<ol>
<li><a href='http://beaver6813.com/2009/10/bash-bad-interpreter/' rel='bookmark' title='Bash, Bad Interpreter'>Bash, Bad Interpreter</a></li>
<li><a href='http://beaver6813.com/2010/03/godaddy-mod_rewrite-hell/' rel='bookmark' title='GoDaddy mod_rewrite Hell'>GoDaddy mod_rewrite Hell</a></li>
<li><a href='http://beaver6813.com/2009/10/fetchmp3-com/' rel='bookmark' title='FetchMP3.com'>FetchMP3.com</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://beaver6813.com/2010/04/php-search-and-replace-directory-recursively/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

