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

时间:2021-11-27 04:23:31

I want to display the first 110 characters of a database entry. Pretty easy so far:

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

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

But the above entry has html code in it that's been entered by the client. So it displays:

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

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

Obviously no good.

显然不行。

I just want to strip out all html code, so I need to remove everything between < and > from the db entry THEN display the first 100 chars.

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

Any ideas anyone?

有什么想法吗?

4 个解决方案

#1


99  

use strip_tags

使用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


15  

Use PHP's strip_tags() function.

使用PHP的strip_tags()函数。

For example:

例如:

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


print($businessDesc);

#3


9  

Remove all HTML tags from PHP string with content!

Let say you have string contains anchor tag and you want to remove this tag with content then this method will helpful.

假设你有一个包含锚标记的字符串,你想要删除这个带有内容的标签,那么这个方法会很有用。

$srting = '<a title="" href="/index.html"><b>Some Text</b></a>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.';

echo strip_tags_content($srting);

function strip_tags_content($text) {

    return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);

 }

Output:

输出:

Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Lorem Ipsum只是印刷业和排字业的虚拟文本。

#4


5  

use this regex: /<[^<]+?>/g

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

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

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

from your example should stay: Ref no: 30001

从您的示例中应该保留:Ref no: 30001

#1


99  

use strip_tags

使用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


15  

Use PHP's strip_tags() function.

使用PHP的strip_tags()函数。

For example:

例如:

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


print($businessDesc);

#3


9  

Remove all HTML tags from PHP string with content!

Let say you have string contains anchor tag and you want to remove this tag with content then this method will helpful.

假设你有一个包含锚标记的字符串,你想要删除这个带有内容的标签,那么这个方法会很有用。

$srting = '<a title="" href="/index.html"><b>Some Text</b></a>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.';

echo strip_tags_content($srting);

function strip_tags_content($text) {

    return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);

 }

Output:

输出:

Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Lorem Ipsum只是印刷业和排字业的虚拟文本。

#4


5  

use this regex: /<[^<]+?>/g

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

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

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

from your example should stay: Ref no: 30001

从您的示例中应该保留:Ref no: 30001