Re: replace nested custom html tags

From: Date: Mon, 05 Nov 2001 10:07:13 +0000
Subject: Re: replace nested custom html tags
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-73457@lists.php.net to get a copy of this message
try this snippet? <? function replace_tags(&$text) { $re = "/\[([ubi])\](.*?)\[\/(\\1)\]/msi"; if (preg_match($re, $text)) { $text = preg_replace ($re, "<\\1>\\2</\\1>", $text); replace_tags($text); } } $t = "[b] [u] underlined and bold [/u] [/b] [i] italic [/i]"; replace_tags(&$t); echo $t; ?> "Bruce Brackbill" <brackbillbruce@hotmail.com> wrote in message news:F114pfeChDKhzrMkrx400005e72@hotmail.com... > > Hi, > > Anyone have any ideas on how I could modify > the following to work on nested tags? > > The regex replaces custom tags [i] [u] [b] > with real tags. > > The Regex takes any of the characters in the class \[([ubi])\] > then non greedily matches any character (.*?) then matches > the result of the first reference from the character > class \[\/(\\1)\]/ . It then replaces the [u]stuff[/u] to > <u>stuff</u> etc. > > Everthing works fine, except when the tags are nested. > > I'm actually suprised that you can use a reference ( eg: \\1 ) > in the pattern and not only in the replacement. There does > not seam to be any mention of that anywhere. The use of > references in the pattern does not appear to be related > in any way the the problem. > > > echo preg_replace > ("/\[([ubi])\](.*?)\[\/(\\1)\]/msi","<\\1>\\2</\\1>", > " [b] [u] underlined and bold [/u] [/b] [i] italic [/i]"); > > > The output:( it doesn't replace the nested [u] [/u] ): > > <b> [u] underlined and bold [/u] </b> <i> italics </i> > > Thanks, > Bruce > > > > _________________________________________________________________ > Get your FREE download of MSN Explorer at > http://explorer.msn.com/intl.asp >

« previous php.general (#73457) next »