.大小写不敏感替换字符串中的数字


str_replace. replacing numbers from a string

嗨,这里有这段代码

$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

我需要去掉"-"前面的数字这能做到吗?

谢谢

您可以使用preg_replace()或preg_split()的regex,但我认为explosion()会更好:

$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 */

要删除的数字是固定位数吗?如果是,你可以这样做:

$tags = substr($tags, 9);

否则,如果你确定每个数字都以"-"结尾,你可以这样做:

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

试试这个:

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