如何仅删除字符串中的html标签? [英] How to remove only html tags in a string?

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

问题描述

我已经写了删除HTML标签的代码,但是它也删除了a<b类型的字符串.我希望它不要删除2<3a<b之类的字符串.

I have written code for removing HTML tags, but it is also removing a<b type of strings. I want it to not to remove strings like 2<3 or a<b.

$term="a<b";
echo "Text is--->".preg_replace('/(?:<|&lt;).+?(?:>|&gt;)/', '', $term);

如何在不删除LT或GT的情况下删除字符串中的html标签?

How do I remove html tags in a string, without removing LT or GT?

推荐答案

对不起,我的验证不够.

Sorry I had not validate enough.

我已经在下面检查了php5-cli表达式.

I have checked php5-cli expression below.

(?:<|&lt;)\/?([a-zA-Z]+) *[^<\/]*?(?:>|&gt;)

PHP代码可用:

#!/usr/bin/php 
<?php

$str = "<html></html>
a<b 1<2 3>1 
<body>1>2</body>
<style file=\"'googe'\" alt=\"google\">hello world</style>
<have a good efghijknopqweryuip[]asdfgghjkzxcv bnm,.me>hello world<> google com</s>
<a se=\"font: googe;\">abcde</a>";

echo "text--->".preg_replace('/(?:<|&lt;)\/?([a-zA-Z]+) *[^<\/]*?(?:>|&gt;)/', '', $str)."\n";

?>

结果:

text--->
a<b 1<2 3>1 
1>2
hello world
hello world<> google com
abcde

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

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