Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show bbcode in html page with php

Tags:

php

bbcode

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>

like image 354
user3693999 Avatar asked Nov 21 '25 03:11

user3693999


1 Answers

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.

like image 149
svcd Avatar answered Nov 22 '25 15:11

svcd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!