开发者

BB Code issue PHP

开发者 https://www.devze.com 2023-01-17 16:12 出处:网络
Alright so I am using a little bbcode function for a开发者_StackOverflow社区 forum I have, working well, so if, in example, I put

Alright so I am using a little bbcode function for a开发者_StackOverflow社区 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


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

(note the trailing m)


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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消