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

php
2022-08-30 07:45:58

我想显示数据库条目的前 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个字符。

任何人的任何想法?


答案 1

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) . "..."; ?>

答案 2

使用 PHP 的 strip_tags() 函数

例如:

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


print($businessDesc);

推荐