John C. John C. - 1 month ago 10
JSON Question

PHP JSON Removing HTML Tags

I'm working with an API that returns JSON and I'm using PHP to display it. Inside the JSON is HTML tags. I've been reading about the many ways you can remove them from the returned JSON but I have so many unique tags that I'm wondering what the easiest method would be? A lot of other questions seem to focus on specific tags and finding a solution to remove them. Is it possible to just remove all known HTML tags or do I need to program each one individually? If the answer is yes, what is the method for doing so?

Thank you for your time and input.

Answer

Simply do this:

$data = json_decode($your_json_string, TRUE);

array_walk_recursive($data, function(&$v) { $v = htmlentities($v); });

or to remove tags completely

array_walk_recursive($data, function(&$v) { $v = strip_tags($v); });