2012-05-16 72 views
-1

請有人幫助我。使用php刪除包含3個字符的單詞

我想刪除其中包括3個字符或更少使用PHP代碼的話,請用下面的句子幫助:

$string = "this is my new minimalist style"

的結果必然是「新簡約主義風格」

那麼,我應該用什麼代碼來做到這一點?

+0

是不是第三個字「新」3個字符長? – core1024

+1

他想刪除*少於3個字符 – ilanco

+0

@ core1024:是的,但它表示要求刪除少於三個字符的字符。 –

回答

2

你可以這樣做:

$string = "this is my new minimalist style" 
$string = preg_replace(array('/\b\w{1,2}\b/','/\s+/'),array('',' '),$string); 
1

應該這樣做:

$text = "this is my new minimalist style"; 
$text = preg_replace("/\b(\w{1,2}\s|\s\w{1,2})\b/","", $text); 
+0

thanx所有...終於,我嘗試這個代碼一個它的wo $ string =「這是我的新極簡主義風格」; $ no_char = array('is','my'); $ pattern ='/ \ b(?:'。join('|',$ no_char)。')\ b/i'; $ output = preg_replace($ pattern,'',$ string); –

0

另一個soution爲PHP> = 5.3(也可與舊版本中,你只需要避免匿名函數使用):

$string = "this is my new minimalist style"; 

print implode(" ", array_filter(explode(" ", $string), function($item) { return strlen($item) >= 3; })); 
相關問題