2012-08-14 34 views

回答

1
$id = substr(strstr($name, '_', true), 5); 
1

我會嘗試正則表達式:

preg_match("/(?:\d){6}(\d+)/", "1330001337_jenir.jpg", $matches); 

echo $matches[2]; 
+0

這可能爲'改善/(?:\ d){6}(\ d +)/'。這樣,前6位數字不會以匹配形式返回,然後下一個組就是整組數字 - 不需要檢查下劃線或任何其他字符。 – newfurniturey 2012-08-14 06:55:20

+0

@newfurniturey:謝謝。我不得不停止使用那些非貪婪的捕捉組... – Blender 2012-08-14 07:05:09

0

試試這個代碼塊 -

$mystr = "1330001337_jenir.jpg"; 

echo substr($mystr,6,4); 
0

只需

<?php 
$myStr='1330001337_jenir.jpg'; 

$strArray=explode($myStr,'-'); 
$numStr=substr($strArray[0],6); //returns 1337 

$nameStr=substr($strArray[1],0,-4); //returns jenir 

?>