2011-09-30 37 views
1

HI更換號碼的所有我這裏有這個代碼str_replace。從字符串

$pathinfo = pathinfo($fullpath); 
$tags = $shortpath; 
$tags = str_replace("/", " ", $tags); 
$tags = str_replace("__", " ", $tags); 
$tags = str_replace(".png", "", $tags); 
$tags = str_replace(".jpg", "", $tags); 
$tags = str_replace(".jpeg", "", $tags); 
$tags = str_replace(".gif", "", $tags); 

,一切工作正常與上面,但我也需要在文件開始更換一些數字我加入

例子文件將是

247991 - my_small_house.jpg

其號碼前加「 - 」我需要去 可以這樣做?

感謝

回答

2

您可以使用的preg_replace)正則表達式(或使preg_split(),但我認爲爆炸()會更好:

$chunks = explode('-',$shortpath); // you just keep the part after the dash 
$tags = str_replace(array('/','__'),' ', $chunks[1]); 
$tags = str_replace(array('.png','.jpg','.jpeg','.gif'),'',$tags); 
/* used array to avoid code repetition */ 
+0

謝謝!工作現場:) – VK27

0

試試這個:

preg_replace('/^[0-9]+(.+)/', '$1', $tags); 
+0

您好感謝可悲的是,沒有工作因爲它仍然在添加數字。 – VK27

1

您需要刪除的號碼是由固定數字的數字組成的嗎? 如果是這樣,你可以只是做:

$tags = substr($tags, 9); 

否則,如果你是確保每一個數字結尾「 - 」,你可以這樣做:

$tags = substr($tags, strrpos($tags," - ") + 3);