SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem w/ nested tags

    I am building a safe tags to html converter and I am having the problem with nested tags.

    Code:
    [ B ]bold tag[ B ]nested bold tag[ B ]another nested tag[ /B ][ /B ][ /B ]
    ignore the spaces within the [ ] tags.

    Anyways, if I use a greedy regular expression it only replaces the first and last block, leaving all the nested tags as they are. If I use a non-greedy, then the first [ B ] tag gets replaced, but with the first [ /B ]. THIS COMPLETELY MESSES UP EVERYTHING.

    PHP Code:
    //Greedy
    $t preg_replace('#\[B\](.+)\[\/B\]#','<B>$1</B>',$t);

    //Non-Greedy
    $t preg_replace('#\[B\](.+?)\[\/B\]#','<B>$1</B>',$t); 
    So, using regular expressions, how do I make it replace all the tags?

    Why do I want to use regular expressions? Well because I want to validate that there is an opening tag and a closing tag, and if I use a standard regular expression to only replace the [TAG] or [/TAG] with <TAG> or </TAG> then it wouldn't validate the text. Therefore, if someone was to have text containing a tag that does not close, then it would extend "unclosed" throughout the rest of page until it comes across another </B> tag.
    I can't believe I ate the whole thing

  2. #2
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't do this with regular expressions only, you would have to write a parser or look here or google there
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  3. #3
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet!

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kleineme View Post
    You can't do this with regular expressions only
    Why not?

    PHP Code:
    $text "[b] A [i] B [b] Cc [/b] b [/i] a [/b]";

    $re "~
        \[ (\w+) \]
        
        (
            (. (?! \[ \\1 ) ) *?
        )
        
        \[ / \\1 \]
    ~sx"
    ;

    preg_match($re$text$m);


    while(
    preg_match($re$text$m))
        
    $text preg_replace($re"<$1>$2</$1>"$text);

    header("Content-type: text/plain");
    echo 
    $text


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •