MyBB Community Forums

Full Version: Filtering Post Contents
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
For the Random Quotes module in ASB the script uses several methods to try and remove all BBCode and HTML from the post message before display, but ending quote tags ([/quote]) and img codes are still showing.

I have looked through class parser and have tried text_parse_message() method and regex filters, strip_tags and parse_message() method (like is used in search.php for the sample text) but still there are erratic issues and 'leaked' bbcode.

Is there a fool-proof way to parse a post message and leave only
  • standard text (no size, color or fonts)
  • bold and italics*
  • smilies

* I can live without bold and italics

Is there a standard MyBB function I am missing?

Any help would be appreciated.
I don't know whether if a MyBB core function exists for this, but there are several method you could use:
  • preg_replace: just find and replace all HTML tags, stripping them off;
  • native strip_tags($data) function: PHP can try to automatically strip tags off with this native function. You can also specify allowed tags passing them as second parameter, so it would be the best solution for you;
  • innerText method: if you're using Javascript, this method returns an HTML-free string.

I personally use innerText method in iDLChat but I use a front-end script to do it. I'm also using strip_tags($data) function but for certain strings it may fail. I didn't tested preg_replace but with a nice pattern it should work just fine.
Try taking a look at ZingaBurga's thread tooltip. He uses some parser methods to strip tags. Not sure if it works on images though.
Wouldn't this work?

$text = $parser->parse_message($text);
$text = strip_tags($text);

// maybe run the parser again now for smilies only - not too good for resources though...
(2013-02-07, 07:37 PM)Shade Wrote: [ -> ]
  • preg_replace: just find and replace all HTML tags, stripping them off;
  • native strip_tags($data) function: PHP can try to automatically strip tags off with this native function. You can also specify allowed tags passing them as second parameter, so it would be the best solution for you;
  • innerText method: if you're using Javascript, this method returns an HTML-free string.

I started out using regex modified from class parser. It was just too bulky and in the end still let tags slip through.

As to strip_tags, I am currently using that, but still I get trash. :s

(2013-02-07, 07:40 PM)Leefish Wrote: [ -> ]Try taking a look at ZingaBurga's thread tooltip. He uses some parser methods to strip tags. Not sure if it works on images though.

Okay I will check it out. Thanks Smile

(2013-02-07, 07:47 PM)Euan T. Wrote: [ -> ]Wouldn't this work?

$text = $parser->parse_message($text);
$text = strip_tags($text);

// maybe run the parser again now for smilies only - not too good for resources though...

That is basically what I am doing, but I still get closing quote tags and img codes from time-to-time.

This is the overly-complicated (and still not successful) code I am using right now:

		// Build a post parser
		require_once MYBB_ROOT."inc/class_parser.php";
		$parser = new postParser;

		$parser_options = array(
			'allow_html' => 1,
			'allow_mycode' => 1,
			'allow_smilies' => 0,
			'allow_imgcode' => 1,
			'filter_badwords' => 1,
			'me_username' => $user['username']
		);
		$new_message = strip_tags($parser->parse_message(adv_sidebox_strip_quotes($rand_post['message'])));

		$parser_options_smilies = array(
			"allow_smilies" => 1,
			'allow_mycode' => 1
		);

		$new_message = $parser->parse_message($new_message, $parser_options_smilies);
That's odd. You shouldn't be getting the closing [quote ]s at all. Does it show in MyCOde form I take it?
It is meant to just be a plain-text preview of the post contents but these stray tags keep slipping through.

bizarella posted some screen shots in the ASB thread. In his example it is full img code with open and closing tags. Undecided
Try this function:

function remove_bbcode($str) {
   return preg_replace('|[[\/\!]*?[^\[\]]*?]|si', '', $str);
}

Bold and italics (aka formatting) won't be parsed with this though.
I gave it a shot . . . not bad! Smile

Thanks crazy4cs Smile

Still finding a little trash though (end quotes still Sad ) but it is cool because I can just add another line to strreplace the end quote with '' . . . this should work.

Thanks to all the advice.
Weird. The end quotes should be stripped with this.
Pages: 1 2