我想显示数据库条目的前110个字符.到目前为止很容易:
<?php echo substr($row_get_Business['business_description'],0,110) . "..."; ?>
Run Code Online (Sandbox Code Playgroud)
但是上面的条目中有html代码,由客户输入.所以它显示:
<p class="Body1"><strong><span style="text-decoration: underline;">Ref no:</span></strong> 30001<strong></stro...
Run Code Online (Sandbox Code Playgroud)
显然没有好处.
我只想删除所有的html代码,所以我需要从db条目中删除<和>之间的所有内容然后显示前100个字符.
任何人的想法?
Yog*_*har 123
使用 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) . "..."; ?>
Run Code Online (Sandbox Code Playgroud)
EM-*_*ons 15
使用PHP的strip_tags()函数.
例如:
$businessDesc = strip_tags($row_get_Business['business_description']);
$businessDesc = substr($businessDesc, 0, 110);
print($businessDesc);
Run Code Online (Sandbox Code Playgroud)
Muh*_*zad 10
假设您有字符串包含锚标记,并且您想要删除带有内容的标记,那么此方法将有所帮助.
$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.*?>.*?</\1>@si', '', $text);
}
Run Code Online (Sandbox Code Playgroud)
输出:
Lorem Ipsum只是印刷和排版行业的虚拟文本.
使用这个正则表达式: /<[^<]+?>/g
$val = preg_replace('/<[^<]+?>/g', ' ', $row_get_Business['business_description']);
$businessDesc = substr(val,0,110);
Run Code Online (Sandbox Code Playgroud)
从你的例子应该留下来: Ref no: 30001
| 归档时间: |
|
| 查看次数: |
172805 次 |
| 最近记录: |