从 php 字符串中删除所有 html 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14684077/
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
Remove all html tags from php string
提问by jimbeeer
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?
任何人的想法?
回答by Yogesh Suthar
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) . "..."; ?>
回答by EM-Creations
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);
回答by Muhammad Shahzad
Remove all HTML tags from PHP string with content!
从带有内容的 PHP 字符串中删除所有 HTML 标签!
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.*?>.*?</>@si', '', $text);
}
Output:
输出:
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum 只是印刷和排版行业的虚拟文本。
回答by Maxim Shoustin
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
回答by David G.
For my this is best solution.
对我来说,这是最好的解决方案。
function strip_tags_content($string) {
// ----- remove HTML TAGs -----
$string = preg_replace ('/<[^>]*>/', ' ', $string);
// ----- remove control characters -----
$string = str_replace("\r", '', $string);
$string = str_replace("\n", ' ', $string);
$string = str_replace("\t", ' ', $string);
// ----- remove multiple spaces -----
$string = trim(preg_replace('/ {2,}/', ' ', $string));
return $string;
}
回答by perfectionist1
Just in case, fgetss()read file in a line removing/stripping all html and php tags.
以防万一,fgetss()读取一行中的文件,删除/剥离所有 html 和 php 标签。
回答by Krishnamoorthy Acharya
In laravel you can use following syntax
在 Laravel 中,您可以使用以下语法
@php
$description='<p>Rolling coverage</p><ul><li><a href="http://xys.com">Brexit deal: May admits she would have </a><br></li></ul></p>'
@endphp
{{ strip_tags($description)}}

