1/09/2021  •  2071 views

* A simple PHP BBCode Parser function
* @author Afsal Rahim
* @link

//BBCode Parser function

function showBBcodes($text) {
	// NOTE : I had to update this sample code with below line to prevent obvious attacks as pointed out by many users.
	// Always ensure that user inputs are scanned and filtered properly.
	$text = htmlspecialchars($text, ENT_QUOTES, $charset);

	// BBcode array
	$find = array(

	// HTML tags to replace BBcode
	$replace = array(
	'<span style="text-decoration:underline;">$1</span>',
	'<span style="font-size:$1px;">$2</span>',
	'<span style="color:$1;">$2</span>',
	'<a href="$1">$1</a>',
	'<img src="$1" alt="" />'

	// Replacing the BBcodes with corresponding HTML tags
	return preg_replace($find,$replace,$text);

// How to use the above function:

$bbtext = "This is [b]bold[/b] and this is [u]underlined[/u] and this is in [i]italics[/i] with a [color=red] red color[/color]";
$htmltext = showBBcodes($bbtext);
echo $htmltext;

 English / Indonesian