1

我正在尝试让我的网站提要正常工作。

我需要选择一些内容并显示在我的提要中。选择后,我剥离标签然后显示。

问题是这样的:

数据仍然显示,好像标签仍然存在(但没有可见的 html 标签),例如。剥离后,在我的来源中,我有:

你好(只是说明)

----中间会有空隙,好像html字符仍然存在,但是当我查看我的源时看不到任何-----

你好

我怎样才能解决这个问题 。谢谢

编辑:

为了更清楚,剥离后我仍然得到这样的文本:

这是我的第一行

这是我的第二行,第一行和第二行之间有一个间隙,好像有一个段落标签

更新

我正在使用这个:

$body=substr(strip_tags(preg_replace('/\n{2,}/',"\n",$row["post_content"])),0,150);

当我回显 $body 时,它仍然保持新行

4

3 回答 3

2

在您剥离的结束标签之后,您可能\n在段落的末尾有一个。

preg_replace('/[\p{Z}\s]{2,}/s',' ',$string); 

将删除所有white space,tabsnew linesdouble spaces替换为单个空格。

\s匹配任何空白字符。相当于 Unicode 字符类别[\f\n\r\t\v\x85\p{Z}]。如果使用 ECMAScript 选项指定符合 ECMAScript 的行为,\s则等效于[ \f\n\r\t\v].

于 2011-05-28T22:21:14.603 回答
1

strip_tags will literally strip the tags, leaving any other whitespace behind. You could get rid of extra newlines and whitespace with regular expressions, but depending on your content, you might mangle it.

Remove newlines:

$string = preg_replace('/\n{2,}/',"\n",$string);

Remove extra spaces:

$string = preg_replace('/ {2,}/',' ',$string);
于 2011-05-28T22:05:14.460 回答
0

我正在经历一些非常令人讨厌的相似之处。用修剪解决

    $body=strip_tags(trim($row["post_content"]));
于 2017-07-27T19:37:21.250 回答