Posted By

ZaiSL on 01/08/11


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

pytheas


Strip HTML Tags Function for PHP


 / Published in: PHP
 

striptags is a default HTML Tag stripper in PHP. However, it can't strip some of the tags, so this enhanced version called striphtml_tags will remove more html elements.

  1. function strip_html_tags( $text )
  2. {
  3. $text = preg_replace(
  4. // Remove invisible content
  5. '@<head[^>]*?>.*?</head>@siu',
  6. '@<style[^>]*?>.*?</style>@siu',
  7. '@<script[^>]*?.*?</script>@siu',
  8. '@<object[^>]*?.*?</object>@siu',
  9. '@<embed[^>]*?.*?</embed>@siu',
  10. '@<applet[^>]*?.*?</applet>@siu',
  11. '@<noframes[^>]*?.*?</noframes>@siu',
  12. '@<noscript[^>]*?.*?</noscript>@siu',
  13. '@<noembed[^>]*?.*?</noembed>@siu',
  14. // Add line breaks before and after blocks
  15. '@</?((address)|(blockquote)|(center)|(del))@iu',
  16. '@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
  17. '@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
  18. '@</?((table)|(th)|(td)|(caption))@iu',
  19. '@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
  20. '@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
  21. '@</?((frameset)|(frame)|(iframe))@iu',
  22. ),
  23. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  24. "
  25. $0", "
  26. $0", "
  27. $0", "
  28. $0", "
  29. $0", "
  30. $0",
  31. "
  32. $0", "
  33. $0",
  34. ),
  35. $text );
  36.  
  37. // you can exclude some html tags here, in this case B and A tags
  38. return strip_tags( $text , '<b><a>' );
  39. }

Report this snippet  

You need to login to post a comment.