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('/(<!--.*?-->)/is',array($this,'createSpanTag'),$string);
preg_match_all('/(<[\w\d]+(\s+[\w\d]+(=("[^$4]+$4)?){0,}>)/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('/(\<\;[\w\d]\>\;)/is','$this->createSpanTag',$totalTag);
else
{
$totalTag=preg\_replace\_callback('/(\<\;[\w\d])/is','$this->createSpanTag',$totalTag);
$totalTag=preg\_replace\_callback('/([\w\d]\=)/is','$this->createSpanTag',$totalTag);
$totalTag=preg\_replace\_callback('/(("|\')[^\4]+\4)/is','$this->createSpanTag',$totalTag);
$totalTag=preg\_replace\_callback('/(\>\;)/is','$this->createSpanTag',$totalTag);
}
$string=str\_replace($result[0][$i],$totalTag,$string);
}
return $string;
}
function createSpanTag($result)
{
if(strpos($result[0],'<!--') and strpos($string,'-->'))
$colour=$this->htmlColours['comment'];
elseif(preg\_match('/^\<\;[\w\d](\>\;)?$/is',$result[0]) or $result[0]=='>')
$colour=$this->htmlColours['tag'];
elseif(preg\_match('/^[\w\d]\=$/is',$result[0]))
$colour=$this->htmlColours['attribute'];
elseif(preg\_match('/^("|\')[^\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>