mod: PREG_MATCH - Suchmusterwiederholung

Beitrag lesen

Die Verwendung der Funktion htmlentities() ist in den allermeisten Fällen unsinnig. Verwende htmlspecialchars()!

warum

Ah, wohl das hier:

<a href="index" title='index'>

ich möchte mit preg_match() nach "href="index"" und "title='index'" suchen lassen.

Warum mit regulären Ausdrücken, wenn du ein starres Suchmuster hast, also mit mit Stringfunktionen wie strpos() oder besser mb_strpos()

ich arbeite an einem html-highlightler, und möchte genannte an eine callback-funktion übergeben. der oben genannte ausdruck ist nur zu testzwecken. natürlich sollen auch andere tags gehighlightet werden.

Der komplette Code:
<code>
function highlightHTMLCode($string)
{
  $string=htmlentities($string,ENT_QUOTES);
$string=preg_replace_callback('/(&lt;!--.*?--&gt;)/is',array($this,'createSpanTag'),$string);
preg_match_all('/(&lt;[\w\d]+(\s+[\w\d]+(=(&quot;[^$4]+$4)?){0,}&gt;)/is',$string,$result,PREG_PATTERN_ORDER);

	for($i=0,$iMax=count($result[0]);$i<$iMax;$i++)  
	{  
		$totalTag=$result[0][$i];  
		  
		if(!strpos($totalTag,' '))  
			$totalTag=preg\_replace\_callback('/(\&lt\;[\w\d]\&gt\;)/is','$this->createSpanTag',$totalTag);  

		else  
		{  
			$totalTag=preg\_replace\_callback('/(\&lt\;[\w\d])/is','$this->createSpanTag',$totalTag);  
			$totalTag=preg\_replace\_callback('/([\w\d]\=)/is','$this->createSpanTag',$totalTag);  
			$totalTag=preg\_replace\_callback('/((&quot;|\')[^\4]+\4)/is','$this->createSpanTag',$totalTag);  
			$totalTag=preg\_replace\_callback('/(\&gt\;)/is','$this->createSpanTag',$totalTag);  
		}  

		$string=str\_replace($result[0][$i],$totalTag,$string);  
	}  

	return $string;  
}  

function createSpanTag($result)  
{  
	if(strpos($result[0],'&lt;!--') and strpos($string,'--&gt;'))  
		$colour=$this->htmlColours['comment'];  
		  
	elseif(preg\_match('/^\&lt\;[\w\d](\&gt\;)?$/is',$result[0]) or $result[0]=='&gt;')  
		$colour=$this->htmlColours['tag'];  
		  
	elseif(preg\_match('/^[\w\d]\=$/is',$result[0]))  
		$colour=$this->htmlColours['attribute'];  

	elseif(preg\_match('/^(&quot;|\')[^\1]+\1$/is',$result[0]))  
		$colour=$this->htmlColours['value'];  

	else  
		$colour=$this->htmlColours['default'];  

	$spanTag=$this->htmlSpanStyleTag;  
	$spanTag=str\_replace('{$COLOUR}',$colour,$spanTag);  
	$spanTag=str\_replace('{$CONTENT}',$result[0],$spanTag);  
	return $spanTag;  
}  

</code>