正如标题所问的:
如何在PHP中删除所有不必要的单引号之间的空格,跳过转义引号?
我正在寻找一个快速的实现来为解析做准备。如果正则表达式比使用简单的循环慢,我不喜欢使用正则表达式。
(下面的双引号仅用于显示)
示例如下:
输入:
" testing ' this is a \'test\' ' zzz "输出:
"testing ' this is a \'test\' ' zzz"发布于 2010-01-12 11:55:40
<?php
$parts = preg_split('/((?<!\\\\)|(?<=\\\\\\\\))\'/', trim($data));
foreach ($parts as $index => &$part) {
if ($index % 2 == 0) {
$part = preg_replace('/\s{2,}/', ' ', $part);
}
}
echo join('\'', $parts);现在等待我错过的更简单的解决方案:p
发布于 2010-01-12 11:54:55
好了,psuedocode时间:
var shouldtrim = true;
var escaped = false;
foreach char in string
if char is whitespace and lastchar is whitespace and shouldtrim
remove char from string
if char is ' and not escaped
toggle shouldtrim
if char is \
toggle escaped
else
escaped = false发布于 2010-01-12 11:49:23
试试这个:
<?php
$str = " testing ' this is a \'test\' ' zzz ";
echo trim($str," ");
?>https://stackoverflow.com/questions/2046564
复制相似问题