<?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>MyChances.net &#187; str_replace</title>
	<atom:link href="http://www.mychances.net/blog/tag/str_replace/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mychances.net/blog</link>
	<description>Data-driven college admission prediction</description>
	<lastBuildDate>Wed, 14 Jul 2010 01:53:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Recursive str_replace in PHP</title>
		<link>http://www.mychances.net/blog/2008/03/20/recursive-str_replace-in-php/</link>
		<comments>http://www.mychances.net/blog/2008/03/20/recursive-str_replace-in-php/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 08:50:51 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[str_replace]]></category>

		<guid isPermaLink="false">http://www.mychances.net/blog/2008/03/20/recursive-str_replace-in-php/</guid>
		<description><![CDATA[I was looking for a recursive str_replace in php tonight and I couldn&#8217;t find any, so I wrote one. This takes the exact same parameters as str_replace, in the same order. It recursively searches the $subject, replacing $search with $replace, while keeping track of how many times ($count) it has replaced the $subject.



Example:

$count=0;
$string = "; [...]]]></description>
			<content:encoded><![CDATA[<img style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=a6b86431a750edb0d8748e2bf5a8290d&amp;default=' alt='No Gravatar' width=80 height=80/><p>I was looking for a recursive str_replace in php tonight and I couldn&#8217;t find any, so I wrote one. This takes the exact same parameters as str_replace, in the same order. It recursively searches the $subject, replacing $search with $replace, while keeping track of how many times ($count) it has replaced the $subject.</p>
<pre lang="PHP">
<?
function str_replace_recursive($search,$replace,$subject,&#038;$count=0){
	$newcount=$count;
	$subject = str_replace($search,$replace,$subject,$newcount);
	$count = $newcount+$count;
	if(!(strpos($subject,$search)===false)){
		return str_replace_recursive($search,$replace,$subject,$count);
	}else{
		return $subject;
	}
}
?>
</pre>
<p><strong>Example:</strong></p>
<pre>
$count=0;
$string = "; ; I; am; ; ; ; ; become; ; ; ; ; ; ; ; ;";
string = str_replace_recursive('; ;',';',$string,$count);

echo $string;
echo $count;

Outputs:
string --> I; am; become;
count --> 12
</pre>
<p>(Note that this is not the best code to use for this situation; can you write a faster solution to this example using only plain vanilla str_replace?)</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.mychances.net%2Fblog%2F2008%2F03%2F20%2Frecursive-str_replace-in-php%2F&amp;linkname=Recursive%20str_replace%20in%20PHP"><img src="http://www.mychances.net/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.mychances.net/blog/2008/03/20/recursive-str_replace-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
