简体   繁体   English

如何获取没有html标签的文本

[英]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). 新闻字段类型为TEXT(我以内联样式保存新闻)。 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: 现在,当我在网站上显示新闻时,它会以所有样式显示,这很好,但是我的问题是:我要在主页上显示最后10个新闻表,以显示标题和部分新闻,我使用php代码来获取一些信息来自数据库的我返回的新闻值中的字母:

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

when I echo the $str[1]; 当我回显$ 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 . PHP中有一个名为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. 这应该摆脱所有HTML格式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不丢失html标签的情况下获取大文本的一部分? - How to get part of a big text without losing html tags with php? 如何在没有结束标签的自定义动态 html 标签之间获取文本 - How to get text between custom dynamic html tags without end tags 我正在插入我使用tinymce编辑器编写的数据库文本。 然后,如何在没有html标签的情况下获得此文本? - Im Inserting on database text that I write using tinymce editor. Then, how can I get this text without html tags? 如何在PHP中剪切HTML文本而不破坏标签层次 - How to cut an HTML text in PHP without breaking the tags hierarchy 如何从没有标签的HTML字符串中获取数字? - How to get the number out of a HTML string without tags? DOM文档-如何在不带内部标签的标签内获取文本 - DOM Document - How to get the text inside a tag without the inner tags 如何从html和javascript标签和函数获取文本 - How to get text from html and javascript tags and functions 如何获取没有html标签的格式化文本输出(在symfony中)? - how to get a formatted text output with no html tags (in symfony)? 拆分html文本而不会破坏“打开”标签 - Split html text without breaking “open” tags 在不破坏html标签的情况下剪切文本 - Cutting text without destroying html tags
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM