I have written code for removing HTML tags, but it is also removing a type of strings. I want it to not to remove strings like
2<3
or
Let say you have string contains anchor tag and you want to remove this tag with content then this method will helpful.
$srting = '<a title="" href="/index.html"><b>Some Text</b></a> a<b';
echo strip_tags_content($srting);
function strip_tags_content($text) {
return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
}
Output:
a < b
Retrieved from: Remove all html tags from php string
Use strip tags function of php
echo strip_tags($html)
Strip_tags function is good solution.
But if you need regex, use expression below.
(?:<|<)\/?([a-z]+) *[^\/(?:<|<)]*?(?:>|>)
Use strip_tags
//If you want to allow some tags
$term = strip_tags($term,"<b>");
Sorry I had not validate enough.
I have checked php5-cli expression below.
(?:<|<)\/?([a-zA-Z]+) *[^<\/]*?(?:>|>)
PHP code goes:
#!/usr/bin/php
<?php
$str = "<html></html>
a<b 1<2 3>1
<body>1>2</body>
<style file=\"'googe'\" alt=\"google\">hello world</style>
<have a good efghijknopqweryuip[]asdfgghjkzxcv bnm,.me>hello world<> google com</s>
<a se=\"font: googe;\">abcde</a>";
echo "text--->".preg_replace('/(?:<|<)\/?([a-zA-Z]+) *[^<\/]*?(?:>|>)/', '', $str)."\n";
?>
Result:
text--->
a<b 1<2 3>1
1>2
hello world
hello world<> google com
abcde