之間規定我有一個字符串:刪除空格,不包括這些指定的字符
Some string, "it's a nice string". I like it. "some other text"
我想刪除空格,不包括有beetween「:
Somestring,"it's a nice string".Ilikeit."some other text"
我怎麼能進球呢?
之間規定我有一個字符串:刪除空格,不包括這些指定的字符
Some string, "it's a nice string". I like it. "some other text"
我想刪除空格,不包括有beetween「:
Somestring,"it's a nice string".Ilikeit."some other text"
我怎麼能進球呢?
你可以使用正則表達式,或者你可以欺騙和利用explode()
:
阿之間ommit那裏,你打我!幹得好:d – Grexis 2011-12-21 12:06:35
也許吧,但你的解決方案是清潔:) – 2011-12-21 12:15:03
您可以使用PHP str_replace函數的功能實現。請檢查http://php.net/manual/en/function.str-replace.php
我不擅長用正則表達式,因此該方案不使用任何。我會做什麼:
$str = 'Some string, "it\'s a nice string". I like it. "some other text"';
$pieces = explode('"', $str);
for($i = 0; $i < count($pieces); $i += 2){ // Every other chunk is quoted
$pieces[$i] = str_replace(' ', '', $pieces[$i]);
}
$str = implode('"', $pieces);
如果字符串以雙引號開始,PHP將使$pieces
陣列空的第一個元素,所以這應該仍然工作。
通過處理字符串,刪除的空間。查看PHP字符串函數和正則表達式。 – hakre 2011-12-21 11:55:16
你有什麼到目前爲止已經試過?你被拒絕投票,因爲沒有表現出我相信這樣做的任何努力。 – Hammerstein 2011-12-21 12:18:53
是啊,我想正則表達式,但我只能刪除所有空格,我不知道如何標記 – PiKey 2011-12-21 12:45:23