Last active
August 29, 2015 13:57
-
-
Save morningtoast/9741947 to your computer and use it in GitHub Desktop.
phpBB convert bbcode to html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$bbcode = file_get_contents("bbcode.txt"); | |
function bbcodeToHtml($str, $convertBreaks=true) { | |
$replace = array( | |
// bold | |
array( | |
"pattern" => '/\[b:[a-zA-Z0-9]*\](.*)\[\/b:[a-zA-Z0-9]*\]/', | |
"replace" => '<strong>$1</strong>' | |
), | |
// italic | |
array( | |
"pattern" => '/\[i:[a-zA-Z0-9]*\](.*)\[\/i:[a-zA-Z0-9]*\]/', | |
"replace" => '<em>$1</em>' | |
), | |
// strike | |
array( | |
"pattern" => '/\[s:[a-zA-Z0-9]*\](.*)\[\/s:[a-zA-Z0-9]*\]/', | |
"replace" => '<span class="strike">$1</span>' | |
), | |
// font size converts to h2 | |
array( | |
"pattern" => '/\[size=[0-9]*:[a-zA-Z0-9]*\](.*)\[\/size:[a-zA-Z0-9]*\]/', | |
"replace" => '<h2>$1</h2>' | |
), | |
// images | |
array( | |
"pattern" => '/\[img:[a-zA-Z0-9]*\](.*)\[\/img:[a-zA-Z0-9]*\]/', | |
"replace" => '<img src="$1" alt="Diecast Cars, Hot Wheels, Matchbox" />' | |
), | |
// url-wrapped text | |
array( | |
"pattern" => '/\[url=(.*):[a-zA-Z0-9]*\](.*)\[\/url:[a-zA-Z0-9]*\]/', | |
"replace" => '<a href="$1">$2</a>' | |
), | |
// straight url | |
array( | |
"pattern" => '/\[url:[a-zA-Z0-9]*\](.*)\[\/url:[a-zA-Z0-9]*\]/', | |
"replace" => '<a href="$1">$1</a>' | |
), | |
// quote with name of original author | |
array( | |
"pattern" => '/\[quote="([a-zA-Z0-9]*)":[a-zA-Z0-9]*\](.*)\[\/quote:[a-zA-Z0-9]*\]/', | |
"replace" => '<blockquote><strong>$1</strong>$2</blockquote>' | |
), | |
// removes non-emoticon smileys | |
array( | |
"pattern" => '/\<img src="\{SMILIES_PATH\}.*" alt=":[a-zA-Z0-9]*:" .*>/', | |
"replace" => '' | |
), | |
// concert smiley icons into emoticons | |
array( | |
"pattern" => '/\<img src="\{SMILIES_PATH\}.*" alt="(.*)" .*>/', | |
"replace" => '$1' | |
), | |
// removes color | |
array( | |
"pattern" => '/\[color=#[a-zA-Z0-9]*\](.*)\[\/color\]/', | |
"replace" => '$1' | |
), | |
// catchall, strips any left over bbcode tags | |
array( | |
"pattern" => '/\[[a-zA-Z0-9]*:[a-zA-Z0-9]*\](.*)\[\/[a-zA-Z0-9]*:[a-zA-Z0-9]*\]/', | |
"replace" => '$1' | |
) | |
); | |
$patterns = array(); | |
$replaces = array(); | |
foreach ($replace as $regex) { | |
$patterns[] = $regex["pattern"]; | |
$replaces[] = $regex["replace"]; | |
} | |
$html = preg_replace($patterns, $replaces, $str); | |
if ($convertBreaks) { | |
return(nl2br($html)); | |
} else { | |
return($html); | |
} | |
} | |
echo bbcodeToHtml($bbcode); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment