...

View Full Version : Resolved Strip Certain BBcode from Post Regex



conware
07-26-2012, 10:41 AM
Hi guys I wanted to make a function to remove certain bbcode from my user comments only problem is I don't know much regex code.


<?php
function stripBBCode($text, $code, $replace = '') {
$pattern = "/\[?.($code)\]/";
return preg_replace($pattern, $replace, $text);
}
$text = "google (http://google.com) is fine http://www.google.com/image/image.jpg";

echo stripBBCode($text, '(url||img)');
?>


The basic idea is to supply the bbcode I want to remove to my function.
And then use regex and preg_replace to clean the text.
Now the idea is to keep the text between the bbcode so users can still for example go or see the image if the really wanted to.
So for example if I have this text:



google (http://google.com) is fine http://www.google.com/image/image.jpg


And I run the code I want it to become:


google is fine http://www.google.com/image/image.jpg

gvre
07-26-2012, 02:19 PM
function stripBBCode($text, $code)
{
$code = str_replace(",", "|", $code);
$code = str_replace(" ", "", $code);

/**
* replace the text that starts with a [
* followed by an optional /
* followed by any provided bbcode (case-insensitive, non-capturing group)
* followed by 0 or more characters that are not ]
* and ends with ]
**/
$pattern = '#\[/?(?:' . $code . ')[^]]*\]#i';

return preg_replace($pattern, '', $text);
}

$text = "google (http://google.com) is fine http://www.google.com/image/image.jpg";

echo stripBBCode($text, "url,img");

conware
07-26-2012, 06:07 PM
Thanks gvre, you helped me out alot :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum