I already a bbcode string $mybbcode = [b]Hello word[/b]
with php i want to show it with html format in html page.
ex: <div><b>hello word</b><div>
Asked
Active
Viewed 5,800 times
2

user3693999
- 33
- 1
- 4
-
Dear `preg_replace`is what you need, see the [docs](http://php.net/manual/fr/function.preg-replace.php) or a BBcode parsing library, see [here](http://pear.php.net/package/HTML_BBCodeParser2) – GotchaRob Jun 13 '14 at 07:41
-
possible duplicate of [php regex \[b\] to ](http://stackoverflow.com/questions/7545920/php-regex-b-to-b) – rink.attendant.6 Jun 13 '14 at 07:44
-
See http://nbbc.sourceforge.net/ – stealthyninja Jun 13 '14 at 07:45
2 Answers
6
Basically that others already said to you after, but if you search in Google you'll see quicky lot of info about that, and done functions. Here is a sample:
function bbc2html($content) {
$search = array (
'/(\[b\])(.*?)(\[\/b\])/',
'/(\[i\])(.*?)(\[\/i\])/',
'/(\[u\])(.*?)(\[\/u\])/',
'/(\[ul\])(.*?)(\[\/ul\])/',
'/(\[li\])(.*?)(\[\/li\])/',
'/(\[url=)(.*?)(\])(.*?)(\[\/url\])/',
'/(\[url\])(.*?)(\[\/url\])/'
);
$replace = array (
'<strong>$2</strong>',
'<em>$2</em>',
'<u>$2</u>',
'<ul>$2</ul>',
'<li>$2</li>',
'<a href="$2" target="_blank">$4</a>',
'<a href="$2" target="_blank">$2</a>'
);
return preg_replace($search, $replace, $content);
}
Only for lazy programmers ;)
I invite you to search and decide what are the best from all code already done for you project.

svcd
- 124
- 6
0
You will have to use regex to convert BBCodes to HTML : http://www.php.net/manual/en/ref.pcre.php
For example :
$string = preg_replace('#\[b\](.+)\[\/b\]#iUs', '<b>$1</b>', $string);

fdehanne
- 1,658
- 1
- 16
- 32