tags:

views:

128

answers:

1

Okay, I've been working through a set of string replacments for bbcode style tags in my forum, replacing [b] and [i] etc is fairly simple as I can replace them directly without issue.

There are two tags that are giving me problems, as what I need to do with them is more complex. [quote] and [url] are fine, but, I would like to give users the choice of [quote=person_to_quote] and [url=URL]link text[/url], the [quote=] tag needs to be able to be nested too!

So I need to be able to replace the opening tag [quote= then retain the string add the ] show the quote and then end the [/quote]. I can replace the tags wholesale and retain the =person fine, but that is done by me cheating and simple adding the end tag to the text. What I'd really like to be able to do is pull everything between the = and ] store it so I can manipulate it separately.

Currently I'm using an array and simplye replacing the inline text thus:

    "[quote=" is replaced by "<span class=\"quote\">[Quote: ",

and just slapping the non-replaced text back on the end of it, that's ugly though. What I'd like to be able to do is take the code saying [quote=person]some text here[/quote] and turn it into:

    "[quote=" is replaced by "<span class=\"quote\">$person says: ",

where $person would be a variable storing the person's name so it can be replaced dynamically.

Similarly with the URLs I'd like to replace [url=link]link text[/url] and make it able to accept the url and replace it inline so the output is:

"[url=" is replaced by "<a href=$URL>"

with the html a tag already closed, which means stripping the url out, storing it then replacing it after.

So what method do I use to remove the text between = and the closing ] tags so what I pass into the replace array can be passed out and modified accordingly. Also I'm not worried about nesting in the quotes as the span class styling takes care of that, but I do need a function that can deal with any number of quote tags!.Thoughts please.

EDIT:

Just an update, I've solved the things I wanted to do, I modified the code webbiedave gave me and it works:

$output = preg_replace_callback(
'/\[quote=([^\]]+)?\]/',
create_function(
'$matches',
'return \'<span class="quote">\'.$matches[1].\' says: \';'
),
$comment);

then the close tag is picked up through my normal tag replace search afterwards anyway.

+2  A: 

Try preg_replace_callback:

$output = preg_replace_callback(
        '/\[quote=([^\]]+)?\]([^\[]+)?\[\/quote\]?/',
        create_function(
            '$matches',
            'return \'"[quote=" is replaced by "<span class=\"quote\">\'.$matches[1].\' says: ",\';'
        ),
        '[quote=person]some text here[/quote]'
    );
webbiedave
Hmm, that replaces the entire paired tags if I'm reading that right? The output is fine, in that it gives me a span class, but it doesn't need to search for the end quote tags? As both tags are being replaced, it is altering nesting and it won't work on nested quotes. The end tag can be replaced with a second pass so it doesn't need including. So I need to alter that to only replace the `[quote=chris]` part with `<span class="quote">Chris says <br />` adding another replace for the closing tag will remove nesting problems.
TooManyCooks
Ahh, thanks, I was reading it a little wrong, it was searching for the text between the tags but not replacing the end tag, I've taken that bit out and modified it abit. But +1 for the point in the right direction!
TooManyCooks
Glad I could help. Rather than giving you a complete solution, I just wanted to illustrate how the function could be used to solve your problem.
webbiedave
yeah I added the modified code above for reference, thanks for the pointer, regex still scare me!
TooManyCooks