2011-09-30 88 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);