2011-11-23 68 views
2

我怎樣才能放在一個單詞之間的空間,其中一部分是數字另一個是'?像這樣:如何替換...其中一個部分是數字而另一個是'int?

$string = "this is a 2000px height photo"; 
$string = preg_replace('???',' //1',$string); 
echo $string; //this is a 2000 px height photo 

有人可以幫助我嗎?謝謝!

回答

3
$string = preg_replace('/(\d+)([[:alpha:]]+)/', '$1 $2', $string); 
  • \d+匹配任何數字
  • [[:alpha:]]+匹配任何字母(\w+是錯誤的,因爲匹配字母和數字:在preg_replace()呼叫拆分號碼 - 如 '100' 變成'10 0' )
+0

這是很好..但不知何故,這也分裂了sinlge數字(exapmple:「10 images 2000px高度照片」你會得到這個:「1 0 images 2000 px height photo」) – faq

+0

我編輯我的評論 – faq

+0

非常好,只是我在找什麼!謝謝接受它 – faq

相關問題