如何从数组中删除所有html标签?

时间:2022-08-27 17:08:26

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:

在PHP中是否有一个函数可以对数组的所有条目执行正则表达式替换某种操作?我有一个数组,其中包含许多带有文本的html标签,我想删除标签。所以基本上我正在转换这个:

$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?)

问题是我现在应该如何使用它? (我的数组实际上有超过50个条目,并且在每个条目中可能有10个匹配,所以使用preg_replace可能不是可行的方式,或者是吗?)

4 个解决方案

#1


13  

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.

这里不需要正则表达式,只需使用strip_tags()去除所有html标签,然后简单地修剪()输出,例如

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

#2


2  

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

如果你想要正则表达式方法,你可以简单地执行以下操作:

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

#3


0  

array_map() and strip_tags()

array_map()和strip_tags()

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

The same principle goes for trimming.

修剪同样的原则。

#4


0  

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;
    }

#1


13  

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.

这里不需要正则表达式,只需使用strip_tags()去除所有html标签,然后简单地修剪()输出,例如

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

#2


2  

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

如果你想要正则表达式方法,你可以简单地执行以下操作:

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

#3


0  

array_map() and strip_tags()

array_map()和strip_tags()

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

The same principle goes for trimming.

修剪同样的原则。

#4


0  

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;
    }