我如何从数组中删除所有的html标签? [英] How can I remove all html tags from an array?

查看:128
本文介绍了我如何从数组中删除所有的html标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个函数在PHP中做一个数组的所有条目的正则表达式替换类型?

我有一个数组,其中包含大量的HTML标签,其中的文本,我想删除标签。

所以基本上我把这个转换成:

  $ m = [
< div>第一个字符串< / div>,
< table>
< tr>
< td style ='color:red'>
第二串
< / td>
< / tr>
< / table>,
< a href ='/'>
< B>第三个字符串< / B>< br />
< / a>,
]; b




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


$ b pre> /<。+> / sU

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

解决方案

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

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


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

解决方案

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

这篇关于我如何从数组中删除所有的html标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆