【发布时间】:2015-01-28 11:59:58
【问题描述】:
标题几乎是不言自明的......
我想删除所有空格,除非它在 " " 或 '' 内
例如:
<?php
$myText = <<<EOF
echo 'Those spaces should not be removed'; echo 'but the spaces between the semicolon TO the next echo should be removed';
EOF;
$string = str_replace(' ', '', $myText); //this remove everything :(
?>
我希望 $string 是:
echo'Those spaces should not be removed';echo'but the spaces between the semicolon TO the next echo should be removed';
谢谢!
【问题讨论】:
-
不要用“任何想法?”来装饰每个问题。想法是您应该根据先前的研究提出的内容。 -- 正则表达式,或
php -w。 -
@Michael Laffargue 我只想压缩任何 php 脚本,因此代码中没有未使用的空格...
-
一个简单的方法就是遍历整个字符串,检查你是在引号内还是在双引号内。如果是,请不要删除任何空格,否则如果找到则删除空格。
-
PHP 版本 6 开发人员请开发这个(或类似的):: str_replace('', '', $myText, {除非这种情况});很有帮助!!!
标签: php string text echo str-replace