Click to See Complete Forum and Search --> : Regular Expression Recursion
The Hobo
Apr 24th, 2007, 07:22 PM
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:
[asdf]adsfasdf[asdf]adsfadsf[/adsf][/adsf]
I end up with this:
<asdf>adsfasdf[asdf]adsfadsf</adsf>[/adsf]
Or something similiar depending on the modifiers I use.
How can I accomplish replaces like this?
dclamp
Apr 24th, 2007, 07:28 PM
well cant you use str_replace()?
edit:
wrong function... fixed it
The Hobo
Apr 24th, 2007, 07:33 PM
I can, but not straight out of the box. I'd only want to replace if it has a matched . I can ensure this easily with a regular expression.
kows
Apr 24th, 2007, 07:46 PM
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
$text = 'test text heretest2 text2 here2test2';
//i want to return:
// <tag>test text heretest2 text2 here2</tag>test2
echo $text . "\n\n";
echo preg_replace('/\[(.*)\](.*)\[(.*)\](.*)\[\/(.*)\](.*)\[\/(.*)\]/i', "<$1>$2[$3]$4</$5>$6[/$7]", $text);
?>
this returned:
test text heretest2 text2 here2test2
<tag>test text heretest2 text2 here2</tag>test2
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 (http://regular-expressions.info) is a great resource for learning about regular expressions.
dclamp
Apr 24th, 2007, 07:49 PM
oh i see. try this, it might help. Just a quick google search:
http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx
The Hobo
Apr 24th, 2007, 07:55 PM
Sorry, I don't think I explained myself very well. What I am trying to do is replace any occurrence of with <ul></ul>. So if I have 40 instances of these 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:
adsf[list]asdf
Which should be replaced with:
<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).
kows
Apr 24th, 2007, 07:58 PM
can there be lists within the nested lists? eg, to produce:
<ul>
<li>
<ul>
<li>
<ul>
<li>hello!</li>
</ul>
</li>
</ul>
</li>
</ul>
or can there just be two levels of lists?
The Hobo
Apr 24th, 2007, 07:58 PM
I'm guessing I somehow have to use (?R), recursive patterns: php.net/manual/en/reference.pcre.pattern.syntax.php (http://us.php.net/manual/en/reference.pcre.pattern.syntax.php) but for the life of me, I can't figure out how.
The Hobo
Apr 24th, 2007, 08:00 PM
can there be lists within the nested lists? eg, to produce:
<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).
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.