<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: PHP replace only first occurrence of a string match.</title>
	<atom:link href="http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/feed/" rel="self" type="application/rss+xml" />
	<link>http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/</link>
	<description>Focused on Interactive Design, Development and Marketing</description>
	<lastBuildDate>Tue, 07 Feb 2012 08:42:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: jayjay</title>
		<link>http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/comment-page-1/#comment-658</link>
		<dc:creator>jayjay</dc:creator>
		<pubDate>Fri, 24 Jun 2011 11:26:56 +0000</pubDate>
		<guid isPermaLink="false">http://newsourcemedia.com/blog/1969/12/31/php-replace-only-first-occurrence-of-a-string-match/#comment-658</guid>
		<description>whoops only human coding of the top of my head again ;)
unset($v);
should of been --&gt; unset($val);

proves I&#039;m human after all!

:) :)</description>
		<content:encoded><![CDATA[<p>whoops only human coding of the top of my head again <img src='http://newsourcemedia.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
unset($v);<br />
should of been &#8211;&gt; unset($val);</p>
<p>proves I&#8217;m human after all!</p>
<p> <img src='http://newsourcemedia.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  <img src='http://newsourcemedia.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jayjay</title>
		<link>http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/comment-page-1/#comment-657</link>
		<dc:creator>jayjay</dc:creator>
		<pubDate>Fri, 24 Jun 2011 10:57:13 +0000</pubDate>
		<guid isPermaLink="false">http://newsourcemedia.com/blog/1969/12/31/php-replace-only-first-occurrence-of-a-string-match/#comment-657</guid>
		<description>Hi there, solution found, no prob&#039;s! :)
passed var in to an array with a foreach loop - handy for string phasing in loops,
 x2 birds - one stone! ;)
then just a matter of imploding back to variables.

below, working code for generating an array from variables to search-replace the last character in the last occurrence of strings.
=====================================================================
$arr = array(&quot;apples taste delicious,&quot;, &quot;oranges are sweet,&quot;, &quot;lettuce tastes crunchy,&quot;, &quot;bananas are nice in deserts,&quot;, &quot;pears are tasty!,&quot;, &quot;mangoes are great for breakfast,&quot;);
foreach ($arr as &amp;$val){
$val[strlen($val)-1] = &quot;Z&quot;;
$val = ($val. PHP_EOL);
}
unset($v);
$var = implode(&quot;&quot;, $arr); 
echo $var; 
=====================================================================
Some food for coding thought:  If calling $arr = file(&quot;list-of-fruits.txt&quot;); you will will need to set the negative coordinate (read-backward coordinate) to  -3
 ie: $val[strlen($val)-3] = &quot;Z&quot;;

This is what stomped me for while as I thought I was miss-targeting the last character in the string. Array&#039;s generated from a text file carry excessive white space which tricks php (and the observer) into seeing what appears to be the replacement character appended to the back of a string in an array.

php is merry replacing the last white space behind the values, Something to definitely consider if your manipulating values in and array.!

below, working code for generating an array from text file to search-replace the last character in the last occurrence of strings.
(assuming your info lists it&#039;s sentences line by line)
=====================================================================
$arr = array(&quot;list-of-fruits.txt&quot;);
foreach ($arr as &amp;$val){
$val[strlen($val)-3] = &quot;Z&quot;;
}
unset($v);
$var = implode(&quot;&quot;, $arr); 
echo $var; 
=====================================================================

Hope it help you as it did for me! enjoy~ ;)</description>
		<content:encoded><![CDATA[<p>Hi there, solution found, no prob&#8217;s! <img src='http://newsourcemedia.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
passed var in to an array with a foreach loop &#8211; handy for string phasing in loops,<br />
 x2 birds &#8211; one stone! <img src='http://newsourcemedia.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
then just a matter of imploding back to variables.</p>
<p>below, working code for generating an array from variables to search-replace the last character in the last occurrence of strings.<br />
=====================================================================<br />
$arr = array(&#8220;apples taste delicious,&#8221;, &#8220;oranges are sweet,&#8221;, &#8220;lettuce tastes crunchy,&#8221;, &#8220;bananas are nice in deserts,&#8221;, &#8220;pears are tasty!,&#8221;, &#8220;mangoes are great for breakfast,&#8221;);<br />
foreach ($arr as &amp;$val){<br />
$val[strlen($val)-1] = &#8220;Z&#8221;;<br />
$val = ($val. PHP_EOL);<br />
}<br />
unset($v);<br />
$var = implode(&#8220;&#8221;, $arr);<br />
echo $var;<br />
=====================================================================<br />
Some food for coding thought:  If calling $arr = file(&#8220;list-of-fruits.txt&#8221;); you will will need to set the negative coordinate (read-backward coordinate) to  -3<br />
 ie: $val[strlen($val)-3] = &#8220;Z&#8221;;</p>
<p>This is what stomped me for while as I thought I was miss-targeting the last character in the string. Array&#8217;s generated from a text file carry excessive white space which tricks php (and the observer) into seeing what appears to be the replacement character appended to the back of a string in an array.</p>
<p>php is merry replacing the last white space behind the values, Something to definitely consider if your manipulating values in and array.!</p>
<p>below, working code for generating an array from text file to search-replace the last character in the last occurrence of strings.<br />
(assuming your info lists it&#8217;s sentences line by line)<br />
=====================================================================<br />
$arr = array(&#8220;list-of-fruits.txt&#8221;);<br />
foreach ($arr as &amp;$val){<br />
$val[strlen($val)-3] = &#8220;Z&#8221;;<br />
}<br />
unset($v);<br />
$var = implode(&#8220;&#8221;, $arr);<br />
echo $var;<br />
=====================================================================</p>
<p>Hope it help you as it did for me! enjoy~ <img src='http://newsourcemedia.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jayjay</title>
		<link>http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/comment-page-1/#comment-655</link>
		<dc:creator>jayjay</dc:creator>
		<pubDate>Thu, 23 Jun 2011 20:36:58 +0000</pubDate>
		<guid isPermaLink="false">http://newsourcemedia.com/blog/1969/12/31/php-replace-only-first-occurrence-of-a-string-match/#comment-655</guid>
		<description>Hi there admin, just a bit of curiosity :) I&#039;m trying to replace a char on the last text in the string but keep appending to each, so far I&#039;ve coded: 
$var = &#039;abcdef  abcdef abcdef&#039;;
echo preg_replace(&#039;/f/&#039;, &#039;Z&#039;, $var,  strlen($var)-1); //returns: abcdeZ  abcdeZ abcdefZ

but requiring: abcdef  abcdef abcdeZ

any insight on this one? cheers~:)</description>
		<content:encoded><![CDATA[<p>Hi there admin, just a bit of curiosity <img src='http://newsourcemedia.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I&#8217;m trying to replace a char on the last text in the string but keep appending to each, so far I&#8217;ve coded:<br />
$var = &#8216;abcdef  abcdef abcdef&#8217;;<br />
echo preg_replace(&#8216;/f/&#8217;, &#8216;Z&#8217;, $var,  strlen($var)-1); //returns: abcdeZ  abcdeZ abcdefZ</p>
<p>but requiring: abcdef  abcdef abcdeZ</p>
<p>any insight on this one? cheers~:)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mould Maker</title>
		<link>http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/comment-page-1/#comment-623</link>
		<dc:creator>Mould Maker</dc:creator>
		<pubDate>Tue, 29 Mar 2011 08:02:11 +0000</pubDate>
		<guid isPermaLink="false">http://newsourcemedia.com/blog/1969/12/31/php-replace-only-first-occurrence-of-a-string-match/#comment-623</guid>
		<description>Pretty good article . Thank you for your sharing. I just figure out the problem.</description>
		<content:encoded><![CDATA[<p>Pretty good article . Thank you for your sharing. I just figure out the problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/comment-page-1/#comment-554</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Fri, 10 Dec 2010 15:28:43 +0000</pubDate>
		<guid isPermaLink="false">http://newsourcemedia.com/blog/1969/12/31/php-replace-only-first-occurrence-of-a-string-match/#comment-554</guid>
		<description>Use the str_replace function. For example:
// not sure if you need to escape (\) the question mark symbol 
str_replace(&quot;\?&quot;,&quot;new symbol&quot;,$text);
or
str_replace(&quot;?&quot;,&quot;new symbol&quot;,$text);</description>
		<content:encoded><![CDATA[<p>Use the str_replace function. For example:<br />
// not sure if you need to escape (\) the question mark symbol<br />
str_replace(&#8220;\?&#8221;,&#8221;new symbol&#8221;,$text);<br />
or<br />
str_replace(&#8220;?&#8221;,&#8221;new symbol&#8221;,$text);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrey</title>
		<link>http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/comment-page-1/#comment-550</link>
		<dc:creator>Andrey</dc:creator>
		<pubDate>Fri, 03 Dec 2010 15:01:19 +0000</pubDate>
		<guid isPermaLink="false">http://newsourcemedia.com/blog/1969/12/31/php-replace-only-first-occurrence-of-a-string-match/#comment-550</guid>
		<description>And what should I do, when I need to replace symbol &quot;?&quot;, can you say?</description>
		<content:encoded><![CDATA[<p>And what should I do, when I need to replace symbol &#8220;?&#8221;, can you say?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Homeschool</title>
		<link>http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/comment-page-1/#comment-510</link>
		<dc:creator>Homeschool</dc:creator>
		<pubDate>Thu, 26 Aug 2010 15:02:09 +0000</pubDate>
		<guid isPermaLink="false">http://newsourcemedia.com/blog/1969/12/31/php-replace-only-first-occurrence-of-a-string-match/#comment-510</guid>
		<description>I am using a replace function so I can create an unordered list.  Problem is I need to ignore the first occurance because I start my list and then use the replace to close and start the lists.  

Thankfully, I found your post to help me replace the first occurance of a match in the string!  Worked just like I needed it to!</description>
		<content:encoded><![CDATA[<p>I am using a replace function so I can create an unordered list.  Problem is I need to ignore the first occurance because I start my list and then use the replace to close and start the lists.  </p>
<p>Thankfully, I found your post to help me replace the first occurance of a match in the string!  Worked just like I needed it to!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FirstSearchBlue</title>
		<link>http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/comment-page-1/#comment-469</link>
		<dc:creator>FirstSearchBlue</dc:creator>
		<pubDate>Tue, 11 May 2010 00:32:46 +0000</pubDate>
		<guid isPermaLink="false">http://newsourcemedia.com/blog/1969/12/31/php-replace-only-first-occurrence-of-a-string-match/#comment-469</guid>
		<description>Worked like a charm! Thanks a lot man - comes in handy all the time when playing with WordPress content.</description>
		<content:encoded><![CDATA[<p>Worked like a charm! Thanks a lot man &#8211; comes in handy all the time when playing with WordPress content.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jammboo</title>
		<link>http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/comment-page-1/#comment-453</link>
		<dc:creator>jammboo</dc:creator>
		<pubDate>Sun, 04 Apr 2010 13:01:42 +0000</pubDate>
		<guid isPermaLink="false">http://newsourcemedia.com/blog/1969/12/31/php-replace-only-first-occurrence-of-a-string-match/#comment-453</guid>
		<description>Looks like it actually bolded on your website... odd... I got rid of the 1 at the end of the parameters and it works. Does this only work with an old version of PHP or something?</description>
		<content:encoded><![CDATA[<p>Looks like it actually bolded on your website&#8230; odd&#8230; I got rid of the 1 at the end of the parameters and it works. Does this only work with an old version of PHP or something?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jammboo</title>
		<link>http://newsourcemedia.com/blog/php-replace-only-first-occurrence-of-a-string-match/comment-page-1/#comment-452</link>
		<dc:creator>jammboo</dc:creator>
		<pubDate>Sun, 04 Apr 2010 12:51:34 +0000</pubDate>
		<guid isPermaLink="false">http://newsourcemedia.com/blog/1969/12/31/php-replace-only-first-occurrence-of-a-string-match/#comment-452</guid>
		<description>What if I want to bold the first occurrence of a word in a description? I tried using:

&lt;?
// $description holds the entire description
ereg_replace($keyword, &#039;&lt;b&gt;&#039;.$keyword.&#039;&lt;/b&gt;&#039;, $description);
?&gt;

And I get an error: &quot;Warning: Wrong parameter count for ereg_replace()&quot;</description>
		<content:encoded><![CDATA[<p>What if I want to bold the first occurrence of a word in a description? I tried using:</p>
<p>&lt;?<br />
// $description holds the entire description<br />
ereg_replace($keyword, &#039;<b>&#8216;.$keyword.&#8217;</b>&#8216;, $description);<br />
?&gt;</p>
<p>And I get an error: &#8220;Warning: Wrong parameter count for ereg_replace()&#8221;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
