Tuesday, March 23, 2010

function that removes the HTML tags along with their contents in php

function strip_tags_content($text, $tags = '', $invert = FALSE) {

preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);

if(is_array($tags) AND count($tags) > 0) {
if($invert == FALSE) {
return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?@si', '', $text);
}
else {
return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?@si', '', $text);
}
}
elseif($invert == FALSE) {
return preg_replace('@<(\w+)\b.*?>.*?@si', '', $text);
}
return $text;
}
Sample text:
$text = 'sample text with
tags
';

Result for strip_tags($text):
sample text with tags

33 comments:

  1. 說「吃虧就是便宜的人」,多半不是吃虧的人。..................................................

    ReplyDelete
  2. 幽默並不是諷刺,它或許帶有溫和的嘲諷,卻不傷人,它可能是以別人,也可以用自己為對象。........................................

    ReplyDelete
  3. 一時的錯誤不算什麼,錯而不改才是一生中永遠且最大的錯誤............................................................

    ReplyDelete
  4. 河水永遠是相同的,可是每一剎那又都是新的。. . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    ReplyDelete

Fitness Tip of the Day