简体   繁体   English

如何在Twig中提取从HTML解析的纯文本?

[英]How do I extract a plain text parsed from HTML in Twig?

I am making a kind of blog project with Symfony2 . 我正在用Symfony2做一个博客项目。

I create the content of my articles with IvoryCKEditorBundle and then display them with {{ article.content|raw }} in Twig. 我使用IvoryCKEditorBundle创建文章的内容,然后在Twig中使用{{ article.content|raw }}显示它们。

I want to display on my homepage the first lines of the article: To do this, I need to parse the HTML code and extract strings in it. 我想在首页上显示该文章的第一行:为此,我需要解析HTML代码并提取其中的字符串。

How do I extract plain text from HTML, in my Twig template? 如何在Twig模板中从HTML提取纯文本?

Twig offers a bunch of filters that you can use. Twig提供了一堆可以使用的过滤器。 If you want to display only the first line, you can slice your string after a certain number of characters. 如果您只想显示第一行,则可以在一定数量的字符后切字符串。 You should look into the documentation of Twig. 您应该查看Twig的文档。 There is also another filter to strip the tags if you need to do it before slicing your string. 如果需要在切片字符串之前进行剥离,还可以使用另一个过滤器来剥离标签。

{{ '12345'|slice(1, 2) }}
{# outputs 23 #}

http://twig.sensiolabs.org/doc/filters/slice.html http://twig.sensiolabs.org/doc/filters/slice.html

You can get plain text using "striptags" filter then apply "truncate" filter of twig to get portion of your post. 您可以使用“ striptags”过滤器获取纯文本,然后应用嫩枝的“ truncate”过滤器以获取部分帖子。 for more detail please read my answer https://stackoverflow.com/a/29295738/998686 有关更多详细信息,请阅读我的回答https://stackoverflow.com/a/29295738/998686

If you need extra functionality I guess you need to write new twig extension and put any custom logic there. 如果您需要其他功能,我想您需要编写新的树枝扩展并将任何自定义逻辑放在此处。 It do not take much time but allows you to do what you want. 它不需要花费很多时间,但是可以让您做自己想做的事情。 Have a look at http://symfony.com/doc/current/cookbook/templating/twig_extension.html 看看http://symfony.com/doc/current/cookbook/templating/twig_extension.html

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

相关问题 我可以从mysql表中提取html而不是纯文本吗? - Can I extract html from mysql table instead of plain text? 如何从纯文本文件中提取文本块? - How to extract blocks of text from a plain text file? 如何从PHP的Twitter的以下解析数据中获取text参数? - How do I fetch the text parameter from the following parsed data from Twitter in PHP? 如何通过HTML纯文本(又称href URL)通过$ _POST发送数据? - How do I send data via $_POST via a HTML plain-text (aka href URL)? 如何更改从PHP Simple HTML DOM解析器解析的文本? - How to change the text parsed from PHP Simple HTML DOM Parser? 如何修复从 HTML 提取的纯文本的句子间距? - How do you fix sentence spacing on extracted plain text from HTML? 如何将编码从纯文本更改为Unicode,以便可以从HTML读取特殊字符? - How to change encoding from plain text to Unicode so that I can read special characters from a HTML? 如何从PHP的HTML列表中提取结构化文本? - How can I extract structured text from an HTML list in PHP? 如何获取从XML创建的DOM元素的纯文本和HTML? - How can I get the Plain text AND the HTML of a DOM element created from XML? 如何在 Twig 模板中插入值并将纯文本用作字符串? - How to Insert values in a Twig template and use the plain text as a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM