Skip to content

Instantly share code, notes, and snippets.

@ReneeVandervelde
Created March 26, 2011 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReneeVandervelde/888077 to your computer and use it in GitHub Desktop.
Save ReneeVandervelde/888077 to your computer and use it in GitHub Desktop.
Strips HTML from inside PRE tags only
function stripHTML($str) {
preg_match_all('#</?pre(>|\s[^>]*>)(.*?)</pre(>|\s[^>]*>)#is',$str,$matches);
foreach($matches[2] as $match){
$str = str_replace($match, htmlspecialchars($match),$str);
}
$str = preg_replace("/<\?/","&lt;?",$str);
$str = preg_replace("/\?>/","?&gt;",$str);
return $str;
}
@ReneeVandervelde
Copy link
Author

Alternatively you can use strip_html() in place of htmlspecialchars() on line 5 if you want to completely axe the HTML rather than converting it into text. This seems a little useless in the pre tag, so I'm leaving it this way.
You can also replace both the pre's on line 2 with any other tag if you would like to modify the contents of a different tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment