How to remove all html tags from an array?

Is there a function in php to execute a regular expression that replaces the type of action with all entries in the array?
I have an array that contains a lot of html tags with text in them, and I want to remove the tags.
So basically I convert this:

$m = [ "<div>first string </div>", "<table> <tr> <td style='color:red'> second string </td> </tr> </table>", "<a href='/'> <B>third string</B><br/> </a>", ]; 

:

 $m = [ "first string", "second string", "third string" ] 

A regex that (hopefully) matches everything I want to remove is as follows:

 /<.+>/sU 

The question is, how should I use it now? (My array actually has more than 50 entries, and there can be 10 matches in each entry, so using preg_replace is probably not the way to go or is it?)

+6
source share
4 answers

There is no need for a regular expression, just use strip_tags() to get rid of all the html tags and then just trim() output, e.g.

 $newArray = array_map(function($v){ return trim(strip_tags($v)); }, $m); 
+9
source

You can simply do the following if you want to use the regex:

 $array = preg_replace("/<.+>/sU", "", $array); 
+1
source

array_map () and strip_tags ()

 $m = array_map( 'strip_tags', $m ); 

The same principle is suitable for trimming.

0
source

Here's an option for multidimensional arrays with object validation

 /** * @param array $input * @param bool $easy einfache Konvertierung fΓΌr 1-Dimensionale Arrays ohne Objecte * @param boolean $throwByFoundObject * @return array * @throws Exception */ static public function stripTagsInArrayElements(array $input, $easy = false, $throwByFoundObject = true) { if ($easy) { $output = array_map(function($v){ return trim(strip_tags($v)); }, $input); } else { $output = $input; foreach ($output as $key => $value) { if (is_string($value)) { $output[$key] = trim(strip_tags($value)); } elseif (is_array($value)) { $output[$key] = self::stripTagsInArrayElements($value); } elseif (is_object($value) && $throwByFoundObject) { throw new Exception('Object found in Array by key ' . $key); } } } return $output; } 
0
source

All Articles