2012-01-31 102 views
-2

我有这样的代码 -如何删除此空白区域?

<?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。 请帮帮忙,谢谢

+2

但你的代码不会删除它们。或者你想删除这两个空格? – 2012-01-31 09:52:25

+1

什么不适用于您当前的代码? – Bojangles 2012-01-31 09:52:38

+0

你目前有什么不工作?你会得到什么,你期望什么? – DaveRandom 2012-01-31 09:52:59

回答

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

只需使用:

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

这与OP已有的一样。 – dfsq 2012-01-31 09:53:50

+0

@dfsq ...只是不够好,因为它只匹配一种类型的空白字符。 – DaveRandom 2012-01-31 09:55:10

+0

恩,是的,你说得对。我没有看到它,因为我通常在'[]'里面使用chars类。所以,OP代码已经起作用了,实际上... – 2012-01-31 09:56:30

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)); 
?> 
2

尝试这样的事情

str_replace(' ', ' ', $yourString) 

记住它会删除所有的双位太

2

全部更换多个空格,试试这个:

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

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

0

试试这个

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