Created
March 26, 2011 06:31
-
-
Save ReneeVandervelde/888077 to your computer and use it in GitHub Desktop.
Strips HTML from inside PRE tags only
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("/<\?/","<?",$str); | |
$str = preg_replace("/\?>/","?>",$str); | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.