note 85836 deleted from function.preg-replace-callback by danbrown
| From: | danbrown@php.net | Date: | Thu, 18 Feb 2010 14:39:22 +0000 |
| Subject: | note 85836 deleted from function.preg-replace-callback by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166147@lists.php.net to get a copy of this message | ||
Note Submitter: harrrrpo at gmail dot com
----
The last example -nested BBCode- has a problem and won't work
it should be
<?php
$regex = '#\[indent]((?:[^\[]|\[(?!/?indent])|(?R))+)\[/indent]#';
?>
([ should be escaped to be \[)
--
a nicer regex i used in a BBCode parser (BBEngine http://www.phpclasses.org/browse/package/4829.html)
This one Captures Tag Arguments ,Empty tags and
also uses ?> for performance , wrapped in a simple function for templating (ofcourse it's
one line but note's line size is limited :) )
<?php
/**
* A Template for the recursive tags matcher RE
* it generates it for a given tag ,open bracket and closing one
* $O & $C must be pre-escaped from #'s
* @param String $tag Tag to be parsed recursively
* @param String $O Opeening brackets of tag
* @param String $C Closing brackets of tag
*/
public function Recursive_RE_Generator($tag,$O,$C)
{
$re="#{$O}({$tag}.*?){$C}((?>{$O}(?!/?{$tag}[^{$O}]*?{$C})|
[^{$O}]|(?R))*){$O}/{$tag}{$C}#is";
return $re;
}
?>