• 0

Stripping code tags from strings


Question

Hi... I'm coming across a php-problem when trying to strip html and bbcode tags out from certain strings. Here's the code I'm using:

$rawfingy = preg_replace("/\b((http(s?):\/\/)|(www\.))([\w\.]+)([-~\/\w+\.-?]+)\b/i", "", $comment_arr[4]); //to remove links
$rawfingy = preg_replace("/([\w\.]+)(@)([-\w\.]+)/i", "", $comment_arr[4]); //to remove email links
$rawfingy = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/i","",$comment_arr[4]); //to remove quotes
$rawfingy = preg_replace("/\[quote\](.*?)\[\/quote\]/i","",$comment_arr[4]); //to remove quotes
$rawfingy = preg_replace("/:.*?:/i", "", $comment_arr[4]); //to remove the :smilies:
$rawfingy = preg_replace("/\<(.*?)\>(.*?)\<\/(.&?)\>/ims","",$text); //to remove html code tags
$output = str_replace("{comment-raw}", "$rawfingy",$output);

Now it seems that the only thing that is being stripped is the smiley tags between two colons... I'm still getting <br /> , <em> , [link=.. , and [quote=... tags. Can anyone help ?

Link to comment
https://www.neowin.net/forum/topic/275767-stripping-code-tags-from-strings/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Instead of

$rawfingy = preg_replace("/\&lt;(.*?)\&gt;(.*?)\&lt;\/(.&amp;?)\&gt;/ims","",$text); //to remove html code tags

I'd rather use

$rawfingy = strip_tags($text);

Or to avoid tricky cheating (eg, using '<spa<span>>'):

while($rawfingy != strip_tags($text))
$rawfingy = strip_tags($text);

.

I'm not a regex geek, so I can't tell much further, but this, sorry. (And I'm still not sure if this function strips self-closed tags, like '<br/>')

  • 0
  Quote
$rawfingy = preg_replace("[snip]", "", $comment_arr[4]); //to remove links

$rawfingy = preg_replace("[snip]", "", $comment_arr[4]); //to remove email links

$rawfingy = preg_replace("[snip]","",$comment_arr[4]); //to remove quotes

$rawfingy = preg_replace("[snip]","",$comment_arr[4]); //to remove quotes

$rawfingy = preg_replace("[snip]", "", $comment_arr[4]); //to remove the :smilies:

$rawfingy = preg_replace("[snip]","",$text); //to remove html code tags

$output = str_replace("{comment-raw}", "$rawfingy"$output);

See where you're going wrong?

Try:

$rawfingy = preg_replace("/\b((http(s?):\/\/)|(www\.))([\w\.]+)([-~\/\w+\.-?]+)\b/i", "", $comment_arr[4]); //to remove links
$rawfingy = preg_replace("/([\w\.]+)(@)([-\w\.]+)/i", "", $rawfingy); //to remove email links
$rawfingy = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/i", "", $rawfingy); //to remove quotes
$rawfingy = preg_replace("/\[quote\](.*?)\[\/quote\]/i", "", $rawfingy); //to remove quotes
$rawfingy = preg_replace("/:.*?:/i", "", $rawfingy); //to remove the :smilies:
$rawfingy = preg_replace("/\&lt;(.*?)\&gt;(.*?)\&lt;\/(.&amp;?)\&gt;/ims", "", $rawfingy); //to remove html code tags

$output = str_replace("{comment-raw}", $rawfingy, $output);

  • 0

I see... well it all worked except for the strip html tags thing. is there something wrong with it ? All I really need to strip out are <em>.*?</em> and <strong>.*?</strong> and <b>.*?</b> and <br /> tags

edit: I tried the strip_tags and still can't get the above tags stripped.

Edited by Rolando
  • 0

The tag pattern looks a little overcomplicated to me:

/\&lt;[^\&gt;]*?\&gt;/ims

should * get rid of any content that starts with < and ends with >. This (of course) assumes that any < and > that are textual content have been escaped to HTML entities (< and >).

However, if all you really want to remove is those specific tags you mentioned, then perhaps this might do it:

/\&lt;\/?(?:b(r)?|em|i|strong)[^\&gt;]*?\&gt;/ims

* but probably won't.

  • 0

@ mrbester: the first and second one you gave me was able to strip out the <br /> tags but not any of the others ...

I am now using this code:

$search = array ( "/\&lt;\/?(?:b(r)?|em|i|strong)[^\&gt;]*?\&gt;/ims",
  "/\[link=(.*?)\](.*?)\[\/link\]/i",
  "/\[quote=(.*?)\](.*?)\[\/quote\]/i",
  "/\[quote\](.*?)\[\/quote\]/i",
  "/:.*?:/i");

$replace = array ( "",
  "\\2",
  "",
  "",
  "");

$rawfingy = preg_replace($search, $replace, $comment_arr[4]);

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.