2010-01-12 41 views
0

完全按标题问:我如何会删除单引号之间的所有空间跳绳转义引号在PHP

我如何会删除单引号跳绳逃过PHP引号之间的所有不必要的空格?

我正在寻找快速实施来预先准备解析。我宁愿不使用正则表达式,如果它比使用简单的循环要慢。

(下面的双引号仅用于显示的目的)

的例子是:

输入:

" testing ' this is a  \'test\' '  zzz  " 

输出:

"testing ' this is a  \'test\' ' zzz" 
+0

您的例子似乎并不十分清楚。它看起来像你想剥夺双引号之间的多余空间,但不是单引号? – 2010-01-12 03:45:29

+0

你是对的,我会编辑我的帖子。 TY。 – MichaelICE 2010-01-12 03:48:13

回答

-1

试试:

<?php 
$str = " testing ' this is a  \'test\' '  zzz  "; 

echo trim($str," "); 

?> 
+1

这将删除单引号内的空格。不是我在找什么。 – MichaelICE 2010-01-12 03:50:17

0

好吧,伪码时间:

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 
1
<?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

+1

这是如何处理一个包含'\\''的字符串?鉴于撇号没有逃脱。 – 2010-01-12 03:57:43

+0

谢谢匿名,编辑回答支持逃脱反斜杠。 – tanerkuc 2010-01-12 04:07:50