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.