|
-
Apr 24th, 2007, 07:22 PM
#1
Thread Starter
Stuck in the 80s
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?
-
Apr 24th, 2007, 07:28 PM
#2
Re: Regular Expression Recursion
well cant you use str_replace()?
edit:
wrong function... fixed it
My usual boring signature: Something
-
Apr 24th, 2007, 07:33 PM
#3
Thread Starter
Stuck in the 80s
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.
-
Apr 24th, 2007, 07:46 PM
#4
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.
-
Apr 24th, 2007, 07:49 PM
#5
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
-
Apr 24th, 2007, 07:55 PM
#6
Thread Starter
Stuck in the 80s
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).
-
Apr 24th, 2007, 07:58 PM
#7
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?
-
Apr 24th, 2007, 07:58 PM
#8
Thread Starter
Stuck in the 80s
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.
-
Apr 24th, 2007, 08:00 PM
#9
Thread Starter
Stuck in the 80s
Re: Regular Expression Recursion
 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).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|