如何删除此空格?

时间:2012-01-31 09:50:18

标签: php regex

我有这段代码 -

<?php
$json = 'Two large sardines are seasoned and grilled, then served in a light wine sauce with chopped tomato and generous drizzle of olive oil.  Fresh and delicious.';
print_r(preg_replace('/\s+/',' ',$json));
?>

如何删除这些空格b4 Fresh。 请帮忙, 感谢

6 个答案:

答案 0 :(得分:3)

echo str_replace('  ', ' ',$json);

答案 1 :(得分:2)

尝试这样的事情

str_replace('  ', ' ', $yourString)

请记住它也会删除所有双重空格

答案 2 :(得分:2)

要替换所有多个空格,请尝试以下操作:

preg_replace('/[ ]{2,}/', ' ', $json);

编辑说明:{2,}表示两个或多个前面的字符类,在本例中为空格。)

答案 3 :(得分:0)

只需使用:

preg_replace('/[ ]+/', ' ', $json);

答案 4 :(得分:0)

您的代码有效,不是吗?

另外。这样:

<?php
$json = 'Two large sardines are seasoned and grilled, then served in a light wine sauce with chopped tomato and generous drizzle of olive oil.  Fresh and delicious.';
print_r(str_replace("  "," ", $json));
?>

答案 5 :(得分:0)

试试这个

echo str_replace('  ', '',$json);