如何从字符串中删除HTML标签? [英] How to remove HTML tags from strings?

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

问题描述

我希望动态地从字符串中删除一个块,从一个字符<的出现到另一组2个字符/>的出现.这是可能输出的示例:

I am looking to remove a chunk out of a string dynamically, from the occurrence of one character < to the occurrence of another set of 2 characters />. Here's an example of what might be output:

<img src='blah.png' width="300" height="225" />There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slight...

它可能不是img标签,也可能是锚标签内的img,或者根本没有标签.我需要的是标签(如果有)之后的字符串,更具体地说是前50个字.但是我可以将最多50个单词的内容剥离掉,但我只需要撕掉标签即可.而且我不能作弊并使用img {display:none;}来计算字符.

It might not be an img tag, it could be an img inside an anchor tag, or no tags at all. What I'm after is the string after the tags (if any), more specifically the first 50 words. But I have the stripping down to 50 words thing working, i just need the tags ripped out. And I can't cheat and use img {display:none;} as the characters are counted.

使用PHP是否可能?

推荐答案

怎么样:

$string = "<img src='blah.png' width="300" height="225" />There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slight...";
$new_string = preg_replace('/<.*?\/>/','',$string);

不过,如果您只是解析html标签,则正则表达式可能不是最佳选择.但是,如果只是删除一些字符串,那可能会有所帮助.

Still, if you're just parsing html tags, may regexp wouldn't be the best way to go. But if it's just removing some strings, then it may help.

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

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