简体   繁体   中英

How to get text without html tags

I have a Database table to save news. the news field type is TEXT (I save the news with its styles inline). Now when I show the news on the site it shows with all the styles and that is great but my problem is: I want to make last 10 news table on mainpage will show the title and PART OF NEWS i use a php code to get some letters from my returned news value from the database:

$str1 = $cat_news[$i]['news'];
$str1 = wordwrap($str1, 100);
$str1 = explode("\n", $str1);

when I echo the $str[1]; it shows me the styles like this:

letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none;

I want to show the text without any style just some letters from the text followed by ...

There is a function in PHP called strip_tags . You can use it like this:

$str1 = $cat_news[$i]['news'];
$str1 = strip_tags($str1);
$str1 = wordwrap($str1, 100);
$str1 = explode("\n", $str1);

This should get rid of all HTML formatting.

您可以使用strip_tags() ,但是请注意,如果HTML格式不正确等,它可以删除的内容比您想的要多。它不能对格式等进行适当的检查。也可以使用DOMDocument ,但这确实取决于文本和格式的正确性和完整性如何。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM