highlighting skipphtml {SOLVED}

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
nonano
Forum Newbie
Posts: 1
Joined: Mon Jul 10, 2006 12:06 pm

highlighting skipphtml {SOLVED}

Post by nonano »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I've found [url=http://www.phpclasses.org/browse/package/1140/]this php class[/url] to protect markup when highlighting strings. It works pretty well but only if i do not use arrays.

Even more important is to enhance the code that checks for word-borders (can't figure out how to use this \b there ...)

I'd apprecciate your help!

Code: Select all

class highlighting_skipphtml
{
    var $counter;

    function highlighting_skipphtml()
    {
    	$this->counter=0;
    }

	function highlight($words,&$string,$open='<b>',$close='</b>',$doublecheck=0)
	{
		if(is_array($words))
		{
			foreach($words as $word)
			$this->highlight($word,$string,$open,$close,$doublecheck);
		}
              
              // the foolowing line is added to pay attention to word-borders
              $words = '\b' .$words .'\b'; 

              $wordreg=preg_replace('/([\.\*\+\(\)\[\]])/','\\\\\1',$words);
		$string=preg_replace('/(<)([^>]*)('.("$wordreg").')([^<]*)(>)/sei',"'\\1'.preg_replace('/'.(\"$wordreg\").'/i','###','\\2\\3\\4').'\\5'",stripslashes($string));
		$string=preg_replace('/('.$wordreg.')/si',$open.'\\1'.$close,stripslashes($string));
		$string=preg_replace('/###/si',$words,$string);
		if($this->counter>0 && $doublecheck)
		{
			$tc=str_replace('/','\/',$close);
			$string=preg_replace('/('.$open.')([^<]*)('.$open.')([^<]+)('.$tc.')([^<]*)('.$tc.')/si','\\1\\2\\4\\6\\7',$string);
		}
		$this->counter++;
	}

	function dohighlight($words,$string,$open='<b>',$close='</b>',$doublecheck=0)
	{
		$this->highlight($words,$string,$open,$close,$doublecheck);
		return $string;
	}
}
EDIT:
regex solved: see comment in the lines above. Now there is only the arry-thing left ...


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply