Example BBcode parser php

1/09/2021  •  2073 views
 
<?php
function parsebb($body) { 
 $find = array( 
 "/\[b\](.+?)\[\/b\]/is", 
 "/\[i\](.+?)\[\/i\]/is", 
 "/\[u\](.+?)\[\/u\]/is", 
 "/\[s\](.+?)\[\/s\]/is", 
 "/\[c\=(.+?)\](.+?)\[\/c\]/is",
 "/\[img\](.+?)\[\/img\]/is", 
 "/\[size\=(.+?)\](.+?)\[\/size\]/is",
 "/\[code\](.+?)\[\/code\]/is"
 ); 
 
 $replace = array( 
 "<b>$1</b>", 
 "<em>$1</em>", 
 "<span style=\"text-decoration:underline;\">$1</span>", 
 "<s>$1</s>", 
 "<font color=\"$1\">$2</font>",
 "<img src=\"$1\" alt=\"*\" class=\"image\"/>",
 "<span style=\"font-size:$1\">$2</span>",
 "<div class=\"code\"><pre>$1</pre</div>" 
 ); 
 $body = preg_replace($find, $replace, $body); 
 return $body; 
} 
?>

<?php 
$string = "Your [b]bold text[/b] here";
$output = parsebb{$string);

echo $output;
?>
 English / Indonesian