Results 1 to 9 of 9

Thread: Regular Expression Recursion

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Regular Expression Recursion

    How can I setup a regular expression to replace nested text with replacement text. I can't explain it very well, but say I want to replace [asdf] with <asdf>, and my sample text is:

    Code:
    [asdf]adsfasdf[asdf]adsfadsf[/adsf][/adsf]
    I end up with this:

    Code:
    <asdf>adsfasdf[asdf]adsfadsf</adsf>[/adsf]
    Or something similiar depending on the modifiers I use.

    How can I accomplish replaces like this?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Regular Expression Recursion

    well cant you use str_replace()?

    edit:

    wrong function... fixed it
    My usual boring signature: Something

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Regular Expression Recursion

    I can, but not straight out of the box. I'd only want to replace [asdf] if it has a matched [/asdf]. I can ensure this easily with a regular expression.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Regular Expression Recursion

    replace() isn't a function (yes, yes, I'm crude. I know you probably meant str_replace()), and no, if you bothered to look at the sample he provided.. that wouldn't even work in the first place.

    now, I'm not very good with regular expressions, and I really would have to know a lot more about your situation and what you're trying to do before I could even begin to try to provide you with a more detailed expression. However, for all relevant purposes, this would work and convert the strings you used to what you were looking for. It's not very flexible, though. I would probably wait for CornedBee, because he's crazy with regular expressions.

    PHP Code:
    <?php
      $text 
    '[tag]test text here[tag]test2 text2 here2[/tag]test2[/tag]';
      
    //i want to return:
      //       <tag>test text here[tag]test2 text2 here2</tag>test2[/tag]

      
    echo $text "\n\n";

      echo 
    preg_replace('/\[(.*)\](.*)\[(.*)\](.*)\[\/(.*)\](.*)\[\/(.*)\]/i'"<$1>$2[$3]$4</$5>$6[/$7]"$text);
    ?>
    this returned:
    Code:
    [tag]test text here[tag]test2 text2 here2[/tag]test2[/tag]
    
    <tag>test text here[tag]test2 text2 here2</tag>test2[/tag]
    Feel free to elaborate a little more on the type of flexibility you're looking for (and if there is going to be multiple nested tags or not?). In my sample, you NEED to have a nested tag for it to return anything useful. If the nested tags are optional in your case, it would be useless.

    Oh, and mine's also sloppy because I made a 'catch-all,' as I wasn't sure what kind of characters your tags could contain.

    edit: if you want, this is a great resource for learning about regular expressions.

  5. #5
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Regular Expression Recursion

    oh i see. try this, it might help. Just a quick google search:

    http://haacked.com/archive/2004/10/2...matchhtml.aspx
    My usual boring signature: Something

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Regular Expression Recursion

    Sorry, I don't think I explained myself very well. What I am trying to do is replace any occurrence of [list][/list]with <ul></ul>. So if I have 40 instances of these [list] tags, I want to replace all 40 of them. That's all well and good, except for when they're nested (which in HTML would create sub lists), ie:

    Code:
    [list]adsf[list]asdf[/list][/list]
    Which should be replaced with:

    Code:
    <ul>asdf<ul>asdf</ul></ul>
    (Note, this isn't correct HTML-wise, I am handling <li> as well, but that's irrelevant for this example, as those cannot be nested).
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Regular Expression Recursion

    can there be lists within the nested lists? eg, to produce:
    Code:
    <ul>
      <li>
        <ul>
          <li>
            <ul>
              <li>hello!</li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
    or can there just be two levels of lists?

  8. #8

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Regular Expression Recursion

    I'm guessing I somehow have to use (?R), recursive patterns: php.net/manual/en/reference.pcre.pattern.syntax.php but for the life of me, I can't figure out how.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Regular Expression Recursion

    Quote Originally Posted by kows
    can there be lists within the nested lists? eg, to produce:
    Code:
    <ul>
      <li>
        <ul>
          <li>
            <ul>
              <li>hello!</li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
    or can there just be two levels of lists?
    Any level of nesting (although, I know using recursive regular expressions there is a defined limit controlled by the ini).
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width