Re: replace nested custom html tags

From: Date: Mon, 05 Nov 2001 10:25:04 +0000
Subject: Re: replace nested custom html tags
Groups: php.general 
Request: Send a blank email to php-general+get-73463@lists.php.net to get a copy of this message
Thanks for your response. I didn't think anyone was going to touch this one. I do appreciate your snippet, but I am trying to avoid a loop and calling any more regex functions than I have to. I was really hoping that I could get this into one regexp but after scouring the web I do not think it is possible. Anyone else want to take a stab at this? Here it is: In only 1 preg_replace() -without using arrays as arguments- Convert the following line: [p][b] [u] underlined and bold [/u] [/b] [i] italic [/i][/p] Into: <p><b> <u> underlined and bold </u> </b> <i> italic </i></p> ... while making sure that each tag has a corresonding end tag and only replacing the ones that do. Thanks, Bruce
From: "_lallous" <elias_bachaalany@yahoo.com> To: <brackbillbruce@hotmail.com> Subject: Re: replace nested custom html tags Date: Mon, 5 Nov 2001 12:07:13 +0200 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
_________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp

« previous php.general (#73463) next »