1

Possible Duplicate:
php regex [b] to <b>

I'm having trouble with regex, I'm an absolute regex noob. I can't see what's going wrong with trying to convert the HTML back to the 'BBCode'.

Could somebody take a look at the 'unquote' function and tell me the obvious mistake I'm making? (I know it's obvious because I always find the un-obvious errors)

NOTE: I'm not using recursive Regex because I couldn't get my head around it and already started this way round of sorting out the Quotes so they're nested.

<?php
function quote($str){
    $str = preg_replace('@\[(?i)quote=(.*?)\](.*?)@si', '<div class="quote"><div class="quote-title">\\1 wrote:</div><div class="quote-inner">\\2', $str);
    $str = preg_replace('@\[/(?i)quote\]@si', '</div></div>', $str);
    return $str;
}

function unquote($str){
    $str = preg_replace('@\<(?i)div class="quote"\>\<(?i)div class="quote_title"\>(.*?)wrote:\</(?i)div\><(?i)div class="quote-inner"\>(.*?)@si', '[quote=\\1]\\2',  $str);
    $str = preg_replace('@\</(?i)div\></(?i)div\>@si', '[/quote]', $str);
}
?>

This is just some code to help test it:

<html>
<head>
    <style>
    body {
        font-family: sans-serif;
    }
    .quote {
        background: rgba(51,153,204,0.4)  url(../img/diag_1px.png);
        border: 1px solid rgba(116,116,116,0.36);
        padding: 5px;
    }

    .quote-title, .quote_title {
        font-size: 18px;
        margin: 5px;
    }

    .quote-inner {
        margin: 10px;
    }
    </style>
</head>
<body>
    <?php
    $quote_text = '[quote=VCMG][quote=2xAA]DO RECURSIVE QUOTES WORK?[/quote]I have no idea.[/quote]';
    $quoted = quote($quote_text);
    echo $quoted.'<br><br>'.unquote($quoted); ?>
</body>

Thanks in advance, Sam.

0

2 Answers 2

3

Well, you could start by setting you php class to either quote-title or quote_title but keep it consistent.

Then, add a return $str; to your second function and you should be nearly there.

And you can simplify your regex's a little :

function quote($str){
    $str = preg_replace('@\[quote=(.*)\]@siU', '<div class="quote"><div class="quote-title">\\1 wrote:</div><div class="quote-inner">', $str);
    $str = preg_replace('@\[/quote\]@si', '</div></div>', $str);
    return $str;
}

function unquote($str){
    $str = preg_replace('@<div class="quote"><div class="quote-title">(.*) wrote:</div><div class="quote-inner">@siU', '[quote=\\1]',  $str);
    $str = preg_replace('@</div></div>@si', '[/quote]', $str);
    return $str;
}

But beware of replacing with different calls the start and end tags of your quotes. I thin that unquote can create some strange behaviours if you happen to have other bbcode creating </div></div> code.

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

1 Comment

>_> That goddamn return... I knew it was something obvious. And thank you for the simplification, much better!
0

Personally, I take advantage of the fact that the resulting HTML is basically:

<div class="quote">Blah <div class="quote">INCEPTION!</div> More blah</div>

Repeatedly run the regex until there are no more matches:

do {
    $str = preg_replace( REGEX , REPLACE , $str , -1 , $c);
} while($c > 0);

Also, do it as one regex to make this easier:

'(\[quote=(.*?)\](.*?)\[/quote\])is'
'<div class="quote"><div class="quote-title">$1 wrote:</div><div class="quote-inner">$1</div></div>'

2 Comments

I think having read somewhere that using question marks "inverts greediness" of the quantifier, it does not simply make them lazy. So if the quantifiers are lazy because you used the U option, you made them greedy again by using .*? which is not what you would want.
Ah, yeah. I thought the U was the "unicode" one, but that's lowercase u. Okay, will edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.