laravel - 如何限制不包含 html 标签的文本字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28732710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel - How to limit text string not including html tags
提问by amanda chaw
I have a field in database which contains html tags. I want to show that field in the view without showing HTML tags. I used str_limit($value, $limit = 100, $end = '...')
but it shows the html tags also. Any idea?
我在数据库中有一个包含 html 标签的字段。我想在视图中显示该字段而不显示 HTML 标签。我使用过,str_limit($value, $limit = 100, $end = '...')
但它也显示了 html 标签。任何的想法?
采纳答案by cch
Use the strip_tags()
function.
使用该strip_tags()
功能。
For example this filters the <b></b>
tags:
例如,这会过滤<b></b>
标签:
<?php
echo strip_tags("Hello <b>world!</b>");
?>
In your case would be something along the lines of:
在你的情况下会是这样的:
<?php
strip_tags($value);
?>
See the documentation.
请参阅文档。
Check this answer.
检查这个答案。
回答by user2027202827
str_limit only limits the number of characters. What you need to do is parse the database string and remove anything that looks like a tag. I've never used it, but the php strip_tags function might be more what you're looking for.
str_limit 只限制字符数。您需要做的是解析数据库字符串并删除任何看起来像标签的东西。我从未使用过它,但 php strip_tags 函数可能更符合您的要求。