2012-03-01 32 views
0

發現我的文字是這樣刪除之間的文字「」和‘空間’。如果使用正則表達式

Vindu1: Antall: 4, Bredde i mm: , Hoyde i mm: 1. 

如果有一個空的空間,其次是‘,’(' ,')我想刪除所有直到發現收到的文本','

結果會看起來像

Vindu1: Antall 4, Hoyde i mm: 1. 

我知道我應該使用的preg_replace函數來做到這一點,但我不能figue再出什麼是正則表達式

回答

4

你可以這樣做:

preg_replace('/(?<=,|^)[^,]+ ,/', '', $str); 

例子:

$str = "Vindu1: Antall: 4, Bredde i mm: , Hoyde i mm: 1."; 
echo preg_replace('/(?<=,|^)[^,]+ ,/', '', $str); 

輸出:

Vindu1: Antall: 4, Hoyde i mm: 1. 
+0

我來了同一個。 +1。 – Toto 2012-03-01 16:08:49

+0

謝謝,完美:) – FDI 2012-03-01 16:40:14

0

preg_replace('/\\s+,/', ',')應該這樣做

+4

這不就是在逗號之前刪除空白嗎? – Travesty3 2012-03-01 16:03:36

+0

哦哇是的。我完全誤讀了這個問題! – 2012-03-01 16:09:24

0
$text = 'Vindu1: Antall: 4, Bredde i mm: , Hoyde i mm: 1.' 

if(strstr($text, ' ,')) 
{ 

    $text=explode(' ,', $text); 
    $text1 = $text[0]; 
    $text2 = $text[1]; 

    $text = explode(',', $text1); 

    $result = $text[0].','.$text2; 

} 

對於變量命名的道歉,現在想不出任何明智的事情。

相關問題