tags:

views:

28

answers:

3
+1  Q: 

BB Code issue PHP

Alright so I am using a little bbcode function for a forum I have, working well, so if, in example, I put

[b]Text[/b]

it will print Text in bold.

My issue is, if I have that code:

[b]
Text[/b]

Well it will not work, and just print that as it's right now.

Here is an example of the function I am using:

function BBCode ($string) {
$search = array(
    '#\[b\](.*?)\[/b\]#',
);
$replace = array(
    '<b>\\1</b>',
);
return preg_replace($search , $replace, $string);
}

Then when echo'ing it:

.nl2br(stripslashes(BBCode($arr_thread_row[main_content]))).

So my question would be, what is necessary so the BBcode works with everything inside it, but no necessarily on the same line.

In example:

[b]




Text



[/b]

Would simply be

Text

Thank you for any help!

Alex

+2  A: 

You need the multiline modifier, which makes your pattern something like #\[b\](.*?)\[/b\]#ms

(note the trailing m)

Mark E
I tried that, but it doesn't seem to have an effect tho, but thanks for the reply!
Alex Cane
@Alex: To use `.` and match new lines I believe you also need the `s` modifier. (updated post above)
Mark E
Thanks alot it works now, much appreciated!
Alex Cane
+2  A: 

There is actually a pecl extension that parses BBcode, which would be faster and more secure than writing it from scratch yourself.

ryeguy
A: 

I use this... It should work.

$bb1 = array(
"/\[url\](.*?)\[\/url\]/is",
"/\[img\](.*?)\[\/img\]/is",
"/\[img\=(.*?)\](.*?)\[\/img\]/is",
"/\[url\=(.*?)\](.*?)\[\/url\]/is",
"/\[red\](.*?)\[\/red\]/is",
"/\[b\](.*?)\[\/b\]/is",
"/\[h(.*?)\](.*?)\[\/h(.*?)\]/is",
"/\[php\](.*?)\[\/php\]/is"
);

$bb2 = array(
'<a href="\\1">\\1</a>',
'<img alt="" src="\\1"/>',
'<img alt="" class="\\1" src="\\2"/>',
'<a rel="nofollow" target="_blank" href="\\1">\\2</a>',
'<span style="color:#ff0000;">\\1</span>',
'<span style="font-weight:bold;">\\1</span>',
'<h\\1>\\2</h\\3>',
'<pre><code class="php">\\1</code></pre>'
);

$html = preg_replace($bb1, $bb2, $html);
Webarto
Thanks alot! This is very helpful
Alex Cane