0

I developed a class that allows me to have control over many BBCode tags and other special formats and is working well. The issue I'm having is having inner tags the same as an outer tag. For example, a [quote][/quote] within another [quote][/quote] like this:

[quote=Foo]Here is a quote:

[quote=Bar]Test[/quote]

Did that work?[/quote]

The issue is that the regex is finding the inner end [/quote] when it's finding a match for the outer start [quote].

Would I have to split and go line by line to find proper matches or is there an elegant way of doing this?

Here is a small test of how I'm doing it (there is a bit more but functionally this is all it's doing):

$desc = '[quote=Foo]Here is a quote:

[quote=Bar]Test[/quote]

Did that work?[/quote]';

$args = array(
    'tid' => 1234
);

preg_match('`\[(quote)=?(.*?)\]((\r\n|\r|\n|.)+?)\[\/quote\]`i', $desc, $matches);

list($str, $tag, $match, $innertext) = $matches;

$params = explode(';', $match);
$name   = array_shift($params);

$replacement = '<div class="quote">' . $name . '<span class="quote-text">' . $innertext . '</span></div>';

$string = str_replace($str, $replacement, $desc);

Other tags are fine when they are nested within others, just the like tags are finding the match incorrectly.

1 Answer 1

1

Try using a greedy quantifier instead and enable the s dotall modifier.

preg_match('~\[(quote)=?(.*?)\](.+)\[/quote\]~is', $desc, $matches)

Working Demo

Sign up to request clarification or add additional context in comments.

4 Comments

Cool, so that at least doesn't trample over itself which is great. Guess to have a complete solution would be to parse the inner tags recursively also.
Yes, you could also use a recursive regex if needed.
Would I be lucky to have help with that? My regex foo is quite low, know enough to do damage.
Did you have a chance to look into it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.