Strip BBCode Function
by Adam on Feb.17, 2012, under Other Stuff, TheScripters, Web Design
While working on a website, I was integrating an Announcements forum section into the front page and needed to strip away BBCode that was wreaking havoc with displaying only a hundred of the first characters of a post. This is what I came up with (didn’t bother searching online for something else, either)…
|
1 2 3 4 5 6 7 8 9 10 |
< ?php
require "includes/bbcode.php";
function strip_bbcode($string) {
$string = bbcode($string);
$string = strip_tags($string);
$string = htmlspecialchars($string);
$string = trim($string);
return $string;
}
?> |
I didn’t feel like going through the process of instantiating phpBB’s BBCode class because that would require a pretty lengthy file. So, I just used a basic BBCode function instead. But I think it’d probably be easier to just strip out the tags altogether, which is what phpBB does. If you can figure out how to use it, it would probably be better. It uses preg_replace() to strip out the BBCode, where my example simply converted the BBCode to HTML and used PHP’s own HTML stripping functions.
© 2012, AdamH.us. All rights reserved. Please link back if you use it!
![[Google]]( http://www.adamh.us/wp-content/plugins/easy-adsense-lite/google-dark.gif)
