Home > Backend Development > PHP Tutorial > php字符截取与过滤

php字符截取与过滤

WBOY
Release: 2016-06-06 20:50:54
Original
1123 people have browsed it

1月19日,装扮成《白蛇传》中“白娘子”(中)的演员在南京玄奘寺“腊八节品腊八粥”活动上为市民盛粥。

[caption id="attachment_45" align="aligncenter" width="432"]<a href="http://cqtlmx.com/wp-content/uploads/2013/01/baili.jpg"><img class=" wp-image-45  " alt="“白娘子”布施腊八粥(组图)" src="http://cqtlmx.com/wp-content/uploads/2013/01/baili.jpg"    style="max-width:90%"  style="max-width:90%"></a> “白娘子”布施腊八粥(组图)[/caption]

当日是中国传统的腊八节,南京玄奘寺熬制了咸、甜两种口味的上万份腊八粥,众多市民前来品粥祈福。 新华社记者孙参摄
Copy after login
Copy after login

以上代码截取掉HTML标签,图片啊、[caption 啊等。

目前我用的是

<?php echo mb_strimwidth(strip_tags(get_the_content()), 0, 120,"...");?>
Copy after login
Copy after login

问题:貌似HTML标签没有被过滤掉

回复内容:

1月19日,装扮成《白蛇传》中“白娘子”(中)的演员在南京玄奘寺“腊八节品腊八粥”活动上为市民盛粥。

[caption id="attachment_45" align="aligncenter" width="432"]<a href="http://cqtlmx.com/wp-content/uploads/2013/01/baili.jpg"><img class=" wp-image-45  " alt="“白娘子”布施腊八粥(组图)" src="http://cqtlmx.com/wp-content/uploads/2013/01/baili.jpg"    style="max-width:90%"  style="max-width:90%"></a> “白娘子”布施腊八粥(组图)[/caption]

当日是中国传统的腊八节,南京玄奘寺熬制了咸、甜两种口味的上万份腊八粥,众多市民前来品粥祈福。 新华社记者孙参摄
Copy after login
Copy after login

以上代码截取掉HTML标签,图片啊、[caption 啊等。

目前我用的是

<?php echo mb_strimwidth(strip_tags(get_the_content()), 0, 120,"...");?>
Copy after login
Copy after login

问题:貌似HTML标签没有被过滤掉

我觉得这样的替换比较完美,而且兼容其它标签

$content = '1月19日,装扮成《白蛇传》中“白娘子”(中)的演员在南京玄奘寺“腊八节品腊八粥”活动上为市民盛粥。

[caption id="attachment_45" align="aligncenter" width="432"]<a href="http://cqtlmx.com/wp-content/uploads/2013/01/baili.jpg"><img class=" wp-image-45  " alt="“白娘子”布施腊八粥(组图)" src="http://cqtlmx.com/wp-content/uploads/2013/01/baili.jpg"    style="max-width:90%"  style="max-width:90%"></a> “白娘子”布施腊八粥(组图)[/caption]

当日是中国传统的腊八节,南京玄奘寺熬制了咸、甜两种口味的上万份腊八粥,众多市民前来品粥祈福。 新华社记者孙参摄';

preg_replace(array("/]+>/s", "/\[[^\]]+\]/s"), '', $content);
Copy after login

话说[caption]只算是BBcode不算是HTML标签吧,所以你用strip_tags是木有效果的,所以你可以考虑先把BBcode转换为HTML标签再用strip_tags就应该正常了吧。

如果格式都是这么统一的话,可以考虑用下面的代码:

    $s = array('[caption', ']<a array></a><a>');
    str_replace($s, $r, get_the_content());</a>
Copy after login

效率什么的我是不太清楚了,就当为你提供另外一种思路吧~

$pat[0]='/(\[caption)([\s|\S]+)\/>/';
$pat[1]='/\[\/caption\]/';
$str='[caption id="attachment_45" align="aligncenter" width="432"]<a href="http://cqtlmx.com/wp-content/uploads/2013/01/baili.jpg"><img class=" wp-image-45  " alt="“白娘子”布施腊八粥(组图)" src="http://cqtlmx.com/wp-content/uploads/2013/01/baili.jpg"    style="max-width:90%"  style="max-width:90%"></a> “白娘子”布施腊八粥(组图)[/caption]';
echo preg_replace($pat,'',$str);
Copy after login

不知道lz的期望输出结果是什么.
如果是想删除整个shortcode [caption][/caption]
用wp内置的函数remove_shortcode或者remove_all_shortcodes
如果是想保留

"白娘子"布施腊八粥(组图)
Copy after login

也很简单.

$content = wp_strip_all_tags(str_replace(array('[',']'),array(''),$content));
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template