Fehlersuche in einem PHP-Schnipsel, reg. Ausdrücke.
-
Hallo Leute,
da ich zur Zeit keine Zeit habe mich mit regulären Ausdrücken zu beschäftigen,
hab ich kurzerhand zwei Suchfunktionen für Strings selbst geschrieben:function getTag( $tag_begin, $tag_end, $text ) { $t1 = explode( $tag_begin, $text ); if( count( $t1 ) > 1 ) { if( strstr( $t1[ 1 ], $tag_end ) ) { $t2 = explode( $tag_end, $t1[ 1 ] ); return $t2[0]; } } return ''; } function getTagList( $tag_begin, $tag_end, $text ) { $list = array(); $i = 0; while( strstr( $text, $tag_begin ) && strstr( $text, $tag_end ) ) { $list[$i] = getTag( $tag_begin, $tag_end, $text ); $text = str_replace( $tag_begin.$list[$i].$tag_end, '', $text ); $i++; } return $list; }
Das funktioniert eigentlich recht gut.
$text = '{mytag1}Text{mytag1}{mytag3}Was nun?{mytag3}{mytag5}'; $taglist = getTagList( '{', '}', $text ); // $taglist == array( 1=>mytag1, 2=>mytag1, 3=>mytag3, usw. )
Bis auf einen Aufruf der Art:
$text = '<h1>Text</h1><i>Was nun?</i><hr>'; $taglist = getTagList( '\<', '\>', preg_quote($text) );
Die Liste ist leer. Ich hab $text sogar mit preg_quote() versucht zu
entschärfen. Geht aber trotzdem nicht?
-
Muss man da wirklich < und > escapen