Forivin
Forivin

Reputation: 15508

How can I remove all html tags from an array?

Is there a function in php to do a regex replace kind of action on all entries of an array?
I have an array that contains lots of html tags with text in them and I want to remove the tags.
So basically I'm converting 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>",
];

to this:

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

The regex that (hopefully) matches everything I want to remove, looks like this:

/<.+>/sU

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

Upvotes: 13

Views: 16231

Answers (6)

Keopi98
Keopi98

Reputation: 1

This can be achieved on a single line with array_map() and strip_tags()

$newArray = array_map(fn ($value): string => strip_tags($value), $oldArray);

Upvotes: 0

Eugene Kaurov
Eugene Kaurov

Reputation: 2981

For associative multidimensional arrays, I prefer to use array_walk_recursive:

array_walk_recursive(
    $array, 
    function(&$string) {
        if (is_string($string)) {
            $string = trim(strip_tags($string));
        }
    }
);

See documentation: https://www.php.net/manual/en/function.array-walk-recursive.php

If you are sure in your data, then is_string can be skipped like:

array_walk_recursive($stepViewEntity, 'strip_tags');

Upvotes: 1

mrx
mrx

Reputation: 11

Here a variant for multidimensional arrays with object checking


/**
     * @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;
    }

Upvotes: 1

Danijel
Danijel

Reputation: 12689

array_map() and strip_tags()

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

The same principle goes for trimming.

Upvotes: 3

Rizier123
Rizier123

Reputation: 59681

No need for a regex here, just use strip_tags() to get rid of all html tags and then simply trim() the output, e.g.

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

Upvotes: 27

karthik manchala
karthik manchala

Reputation: 13640

You can simply do the following if you want regex approach:

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

Upvotes: 3

Related Questions