<?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 highlight search keywords</title>
	<atom:link href="http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/</link>
	<description>All about integrating PHP with JavaScript and Flash</description>
	<lastBuildDate>Tue, 31 Jan 2012 02:53:55 +0200</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Benjamin</title>
		<link>http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/comment-page-1/#comment-5893</link>
		<dc:creator>Benjamin</dc:creator>
		<pubDate>Wed, 11 Jan 2012 14:53:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.php-help.ro/php/php-highlight-search-keywords/#comment-5893</guid>
		<description>This tutorial is definitely the best I&#039;ve seen for highlighting search strings in search results because it actually replaces the strings with the correct casing.  Many of the other methods I&#039;ve seen online replace the result text with the casing that was typed in the search.

The only issue I had to deal with was if someone started typing nothing but spaces.  It gets a little crazy.</description>
		<content:encoded><![CDATA[<p>This tutorial is definitely the best I&#8217;ve seen for highlighting search strings in search results because it actually replaces the strings with the correct casing.  Many of the other methods I&#8217;ve seen online replace the result text with the casing that was typed in the search.</p>
<p>The only issue I had to deal with was if someone started typing nothing but spaces.  It gets a little crazy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pertneer</title>
		<link>http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/comment-page-1/#comment-5603</link>
		<dc:creator>Pertneer</dc:creator>
		<pubDate>Fri, 30 Sep 2011 15:31:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.php-help.ro/php/php-highlight-search-keywords/#comment-5603</guid>
		<description>This solution seams to have resolved your issue. If you concatenate the string into a new string you eliminate the issue of searching through the new html text that you create in the foreach loop. Then I cleared the $text to prevent any other unforeseen issues.

class highlight
    {
        public $output_text;
 
        function __construct($text, $words)
        {
            $split_words = explode( &quot; &quot; , $words );
			$output = &quot;&quot;;
            foreach ($split_words as $word)
            {
                $color = self::generate_colors();
				if(strlen($word) &lt; 3){
					
				}else{
                	$text = preg_replace(&quot;&#124;($word)&#124;Ui&quot; , &quot;&lt;b&gt;$1&lt;/b&gt;&quot; , $text );
				}
				$output .= $text;
				$text = &quot;&quot;;
            }
            $this-&gt;output_text = $output;
        }
 
        private function rgbhex($red, $green, $blue)
        {
            return sprintf(&#039;#%02X%02X%02X&#039;, $red, $green, $blue);
        }
 
        private function generate_colors()
        {
            $red = rand( rand(60,100) , rand(200,252) );
            $green = rand( rand(60,100) , rand(200,252) );
            $blue = rand( rand(60,100) , rand(200,252) );
 
            $color = self::rgbhex( $red , $green , $blue );
            return $color;
        }
    }</description>
		<content:encoded><![CDATA[<p>This solution seams to have resolved your issue. If you concatenate the string into a new string you eliminate the issue of searching through the new html text that you create in the foreach loop. Then I cleared the $text to prevent any other unforeseen issues.</p>
<p>class highlight<br />
    {<br />
        public $output_text;</p>
<p>        function __construct($text, $words)<br />
        {<br />
            $split_words = explode( &#8221; &#8221; , $words );<br />
			$output = &#8220;&#8221;;<br />
            foreach ($split_words as $word)<br />
            {<br />
                $color = self::generate_colors();<br />
				if(strlen($word) &lt; 3){</p>
<p>				}else{<br />
                	$text = preg_replace(&quot;|($word)|Ui&quot; , &quot;<b>$1</b>&#8221; , $text );<br />
				}<br />
				$output .= $text;<br />
				$text = &#8220;&#8221;;<br />
            }<br />
            $this-&gt;output_text = $output;<br />
        }</p>
<p>        private function rgbhex($red, $green, $blue)<br />
        {<br />
            return sprintf(&#8216;#%02X%02X%02X&#8217;, $red, $green, $blue);<br />
        }</p>
<p>        private function generate_colors()<br />
        {<br />
            $red = rand( rand(60,100) , rand(200,252) );<br />
            $green = rand( rand(60,100) , rand(200,252) );<br />
            $blue = rand( rand(60,100) , rand(200,252) );</p>
<p>            $color = self::rgbhex( $red , $green , $blue );<br />
            return $color;<br />
        }<br />
    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bystwn22</title>
		<link>http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/comment-page-1/#comment-5353</link>
		<dc:creator>bystwn22</dc:creator>
		<pubDate>Wed, 27 Jul 2011 23:46:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.php-help.ro/php/php-highlight-search-keywords/#comment-5353</guid>
		<description>i know its old topic but, its for newers
i just edited and fixed the search highlight problem, now its work with html
        class highlight {
            public $output_text;
            function __construct($text, $words) {
                $split_words = explode(&quot; &quot;, $words );
                foreach ($split_words as $word) {
                    $color = self::generate_colors();
                    $color2 = self::generate_colors2();
                    $text = preg_replace(&quot;&#124;\b($word)\b&#124;se&quot;, &#039;&quot;&lt;b&gt;$1&lt;/b&gt;&quot;&#039;, $text );
                }
                $this-&gt;output_text = $text;
            }
 
            private function rgbhex($red, $green, $blue) {
                return sprintf(&#039;#%02X%02X%02X&#039;, $red, $green, $blue);
            }
 
            private function generate_colors() {
                $red = rand( rand(200,252) , rand(200,252) );
                $green = rand( rand(200,252) , rand(200,252) );
                $blue = rand( rand(200,252) , rand(200,252) );
 
                $color = self::rgbhex( $red , $green , $blue );
                return $color;
            }
            private function generate_colors2() {
                $red = rand( rand(40,100) , rand(100,252) );
                $green = rand( rand(40,100) , rand(100,252) );
                $blue = rand( rand(40,100) , rand(100,252) );
 
                $color = self::rgbhex( $red , $green , $blue );
                return $color;
            }
        }</description>
		<content:encoded><![CDATA[<p>i know its old topic but, its for newers<br />
i just edited and fixed the search highlight problem, now its work with html<br />
        class highlight {<br />
            public $output_text;<br />
            function __construct($text, $words) {<br />
                $split_words = explode(&#8221; &#8220;, $words );<br />
                foreach ($split_words as $word) {<br />
                    $color = self::generate_colors();<br />
                    $color2 = self::generate_colors2();<br />
                    $text = preg_replace(&#8220;|\b($word)\b|se&#8221;, &#8216;&#8221;<b>$1</b>&#8220;&#8216;, $text );<br />
                }<br />
                $this-&gt;output_text = $text;<br />
            }</p>
<p>            private function rgbhex($red, $green, $blue) {<br />
                return sprintf(&#8216;#%02X%02X%02X&#8217;, $red, $green, $blue);<br />
            }</p>
<p>            private function generate_colors() {<br />
                $red = rand( rand(200,252) , rand(200,252) );<br />
                $green = rand( rand(200,252) , rand(200,252) );<br />
                $blue = rand( rand(200,252) , rand(200,252) );</p>
<p>                $color = self::rgbhex( $red , $green , $blue );<br />
                return $color;<br />
            }<br />
            private function generate_colors2() {<br />
                $red = rand( rand(40,100) , rand(100,252) );<br />
                $green = rand( rand(40,100) , rand(100,252) );<br />
                $blue = rand( rand(40,100) , rand(100,252) );</p>
<p>                $color = self::rgbhex( $red , $green , $blue );<br />
                return $color;<br />
            }<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Constantin Boiangiu</title>
		<link>http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/comment-page-1/#comment-5181</link>
		<dc:creator>Constantin Boiangiu</dc:creator>
		<pubDate>Mon, 20 Jun 2011 09:06:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.php-help.ro/php/php-highlight-search-keywords/#comment-5181</guid>
		<description>Don&#039;t quite get it. Can you please detail a bit?</description>
		<content:encoded><![CDATA[<p>Don&#8217;t quite get it. Can you please detail a bit?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lisam</title>
		<link>http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/comment-page-1/#comment-5176</link>
		<dc:creator>lisam</dc:creator>
		<pubDate>Fri, 17 Jun 2011 08:23:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.php-help.ro/php/php-highlight-search-keywords/#comment-5176</guid>
		<description>how can i make this happen to a table listing search?
like when i use while() statement, then the result shows in a table with so many rows.

help :(</description>
		<content:encoded><![CDATA[<p>how can i make this happen to a table listing search?<br />
like when i use while() statement, then the result shows in a table with so many rows.</p>
<p>help :(</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy Marks</title>
		<link>http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/comment-page-1/#comment-5010</link>
		<dc:creator>Andy Marks</dc:creator>
		<pubDate>Thu, 02 Jun 2011 13:35:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.php-help.ro/php/php-highlight-search-keywords/#comment-5010</guid>
		<description>Great post.  One item I needed to add for my site was to ensure ONLY full words were highlighted.  I accomplished this by modifying:


$text = preg_replace(&quot;&#124;($word)&#124;Ui&quot; ,
                           &quot;&lt;b&gt;$1&lt;/b&gt;&quot; , $text );

to:


$text = preg_replace(&quot;&#124;(\b$word\b)&#124;Ui&quot; ,
                           &quot;&lt;b&gt; $1&lt;/b&gt;&quot; , $text );

there are three edits.  adding \b before and after $word are the first and second edit.  lastly, placing a space before $1.

Thanks again for the class, works wonders.</description>
		<content:encoded><![CDATA[<p>Great post.  One item I needed to add for my site was to ensure ONLY full words were highlighted.  I accomplished this by modifying:</p>
<p>$text = preg_replace(&#8220;|($word)|Ui&#8221; ,<br />
                           &#8220;<b>$1</b>&#8221; , $text );</p>
<p>to:</p>
<p>$text = preg_replace(&#8220;|(\b$word\b)|Ui&#8221; ,<br />
                           &#8220;<b> $1</b>&#8221; , $text );</p>
<p>there are three edits.  adding \b before and after $word are the first and second edit.  lastly, placing a space before $1.</p>
<p>Thanks again for the class, works wonders.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: blogDAI &#171; &#62;&#62; Produccions d&#39;en Kotya &#60;&#60;</title>
		<link>http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/comment-page-1/#comment-4512</link>
		<dc:creator>blogDAI &#171; &#62;&#62; Produccions d&#39;en Kotya &#60;&#60;</dc:creator>
		<pubDate>Mon, 24 Jan 2011 00:28:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.php-help.ro/php/php-highlight-search-keywords/#comment-4512</guid>
		<description>[...] de termes trobats als posts retornats. Per aconseguir-ho s&#8217;ha implementat la classe highlight del web [...]</description>
		<content:encoded><![CDATA[<p>[...] de termes trobats als posts retornats. Per aconseguir-ho s&#8217;ha implementat la classe highlight del web [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Udesh Jayakody</title>
		<link>http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/comment-page-1/#comment-4508</link>
		<dc:creator>Udesh Jayakody</dc:creator>
		<pubDate>Fri, 21 Jan 2011 10:37:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.php-help.ro/php/php-highlight-search-keywords/#comment-4508</guid>
		<description>Thanks it works</description>
		<content:encoded><![CDATA[<p>Thanks it works</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Constantin Boiangiu</title>
		<link>http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/comment-page-1/#comment-4434</link>
		<dc:creator>Constantin Boiangiu</dc:creator>
		<pubDate>Tue, 28 Dec 2010 09:21:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.php-help.ro/php/php-highlight-search-keywords/#comment-4434</guid>
		<description>Hmm, no solution for that yet. This is a very old example about what can be done with regular expressions and it&#039;s for demo purposes only. I&#039;ll try and expand that a little, but this will probably happen during the next year, enjoying holidays now.</description>
		<content:encoded><![CDATA[<p>Hmm, no solution for that yet. This is a very old example about what can be done with regular expressions and it&#8217;s for demo purposes only. I&#8217;ll try and expand that a little, but this will probably happen during the next year, enjoying holidays now.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew</title>
		<link>http://www.php-help.ro/php-tutorials/php-highlight-search-keywords/comment-page-1/#comment-4431</link>
		<dc:creator>Andrew</dc:creator>
		<pubDate>Sun, 26 Dec 2010 23:56:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.php-help.ro/php/php-highlight-search-keywords/#comment-4431</guid>
		<description>Hi guys! First of all, good work! I&#039;m trying to create a similar code, with str_replace. I don&#039;t know exactly what the difference is, but i does no matter. The Problem I have, and this code has also, (and haven&#039;t  found a solution for this yet), if you type in some of the following keywords: span, style, backgroung etc. the result breaks. For example: 
$highlight = new highlight($text , &#039;lorem dolor back&#039;);
it&#039;s a big problem i think. And it is caused by the foreach() function.
First it replaces the words &#039;lorem&#039; to: &lt;b&gt;lorem&lt;/b&gt;
Then it searches the word &#039;back&#039; and replaces it, but it searches in the previous text with the HTML tags.
So the result HTML code is that:
&lt;span style=\&quot;&lt;b&gt;back&lt;/b&gt;ground:#AD7CA7;\&quot;&gt;&lt;b&gt;lorem&lt;/b&gt;

If you have some soluton please share with me, Thanks</description>
		<content:encoded><![CDATA[<p>Hi guys! First of all, good work! I&#8217;m trying to create a similar code, with str_replace. I don&#8217;t know exactly what the difference is, but i does no matter. The Problem I have, and this code has also, (and haven&#8217;t  found a solution for this yet), if you type in some of the following keywords: span, style, backgroung etc. the result breaks. For example:<br />
$highlight = new highlight($text , &#8216;lorem dolor back&#8217;);<br />
it&#8217;s a big problem i think. And it is caused by the foreach() function.<br />
First it replaces the words &#8216;lorem&#8217; to: <b>lorem</b><br />
Then it searches the word &#8216;back&#8217; and replaces it, but it searches in the previous text with the HTML tags.<br />
So the result HTML code is that:<br />
&lt;span style=\&quot;<b>back</b>ground:#AD7CA7;\&#8221;&gt;<b>lorem</b></p>
<p>If you have some soluton please share with me, Thanks</p>
]]></content:encoded>
	</item>
</channel>
</rss>

