strip_tags — Strip HTML and PHP tags from a string

This function tries to return a string with all NULL bytes, HTML and PHP tags stripped from a given str. It uses the same tag stripping state machine as the fgetss() function.


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


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

Result for strip_tags_content($text): 
text with 

Result for strip_tags_content($text, ''): 
sample text with 

Result for strip_tags_content($text, '', TRUE); 
text with


I hope that someone is useful :)

asked Aug 28, 2015 in PHP by rajesh
0 votes

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.