首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从php字符串中删除所有html标签

从php字符串中删除所有html标签
EN

Stack Overflow用户
提问于 2013-02-04 17:47:04
回答 8查看 234K关注 0票数 89

我想显示数据库条目的前110个字符。到目前为止还很简单:

<?php echo substr($row_get_Business['business_description'],0,110) . "..."; ?>

但是上面的条目中有客户端输入的html代码。所以它会显示:

<p class="Body1"><strong><span style="text-decoration: underline;">Ref no:</span></strong> 30001<strong></stro...

显然不是很好。

我只想去掉所有的html代码,所以我需要从db条目中删除<和>之间的所有内容,然后显示前100个字符。

有没有人有主意?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2013-02-04 17:48:55

使用strip_tags

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);   //output Test paragraph. Other text

<?php echo substr(strip_tags($row_get_Business['business_description']),0,110) . "..."; ?>
票数 156
EN

Stack Overflow用户

发布于 2013-02-04 17:48:41

使用PHP的strip_tags() function

例如:

$businessDesc = strip_tags($row_get_Business['business_description']);
$businessDesc = substr($businessDesc, 0, 110);


print($businessDesc);
票数 18
EN

Stack Overflow用户

发布于 2013-02-04 17:50:45

使用这个正则表达式:/<[^<]+?>/g

$val = preg_replace('/<[^<]+?>/g', ' ', $row_get_Business['business_description']);

$businessDesc = substr(val,0,110);

从你的例子中应该保留下来:Ref no: 30001

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14684077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档