Last active
November 28, 2019 03:44
-
-
Save rafasashi/59c9448f5467ea427fa3 to your computer and use it in GitHub Desktop.
strip a single html tag
This file contains hidden or 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 strip_single_tag($str,$tag=''){ | |
if(is_array($tag)){ | |
foreach($tag as $t){ | |
$str=strip_single_tag($str,$t); | |
} | |
} | |
else{ | |
$str1=preg_replace('/<\/'.$tag.'>/i', '', $str); | |
if($str1 != $str){ | |
$str=preg_replace('/<'.$tag.'[^>]*>/i', '', $str1); | |
} | |
} | |
return $str; | |
} |
Thanks wolfkang ! What about now?
This is incorrect and dangerous. You cannot strip tags from HTML using regular expressions. Consider how your code will handle stripping div
tags from this input:
<!doctype html>
<html lang="en">
<head><title>foo</title></head>
<body>
<div>
<script>
let x = "</div>"
</script>
</div>
</body>
</html>
You need a DOM parser for working with HTML documents.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
strip_single_tag('<pre>abc</pre>','p') returns 'abc</pre>'.