【发布时间】:2016-06-29 16:56:35
【问题描述】:
我尝试创建类似 sanitize article 功能的东西。我的意思是清理或规范化显示在 html 博客网站中的文章内容的功能。
它必须删除除\n 和 之外的所有空格。所以换行和空格。
我开始了一个想法,通过 addcslashes php 函数对换行符和空格进行编码。然后删除所有出现的空格。然后使用stripslashes。然后用单个替换多个 \x20 出现。因此,对于正则表达式操作来说,它类似于换行符和空格的临时休眠。
但我没有成功,因为输出没有输入中确实存在的换行符。
我的代码不起作用:
<?php
$text = 'first line
second line';
$text = addcslashes($text, "\x20\t");
$text = preg_replace('/\s+/', ' ', $text);
$text = stripslashes($text);
$text = preg_replace('/\s+/', ' ', $text);
var_dump($text);
如何获得?
谢谢
【问题讨论】: