I am looking to convert some HTML tags into strings. I want to be able to provide an array of tags which are to be converted into strings.
The 3 tags I am looking at are: img, iframe and script which should all display as a string when found inside a string.
Example:
Array: $tags = array("img");
String: This is a picture of my dog <img src="......">
I have looked at htmlentities
, htmlspecialchars
, etc. but they all seem to be all or nothing solutions.
Can any one help?
Thanks
Maybe the htmlentities function might help you.
This is not working for non pair tags, but can be easily edited:
function convert_to_entities($data,$tags) {
foreach($tags as $tag) {
while(strpos($data,"<$tag") !== false) {
$reg = "@(.*)<$tag(.*)>(.*)</$tag>(.*)@";
$data = preg_replace($reg,"$1<$tag$2>$3</$tag>$4",$data);
}
}
return $data;
}
The first parameter is the string and second is array of tags.
Have a look at strip_tags:
strip_tags — Strip HTML and PHP tags from a string
I know you're actually looking for a blacklist ("explicitly filter out these tags") but maybe you should rethink and use a whitelist instead ;)
See this related question: strip_tags() function blacklist rather than whitelist