[英]How to remove part of a file's name in a url
我需要從文件名url的開頭刪除一個子字符串。
我需要刪除的子字符串始終是一系列數字,然后是連字符,然后是字gallery
然后是另一個連字符。
例如2207-gallery-
, 2208-gallery-
, 1245-gallery-
等。
我該如何更改:
http://img.pass.com:7710/img.pass.com/img-1/2207-gallery-25171-content_gallery-1428380843.jpg
對此:
http://img.pass.com:7710/img.pass.com/img-1/25171-content_gallery-1428380843.jpg
要替換的子字符串始終是不同的。
這將匹配1個或多個數字,然后是連字符,然后是“ gallery”,然后是連字符:
模式:( 演示 )
/\d+-gallery-/
PHP代碼:( 演示 )
$image='http://img.pass.com:7710/img.pass.com/img-1/2207-gallery-25171-content_gallery-1428380843.jpg';
echo preg_replace('/\d+-gallery-/','',$image);
輸出:
http://img.pass.com:7710/img.pass.com/img-1/25171-content_gallery-1428380843.jpg
這是您的非正則表達式方法:
echo substr($image,0,strrpos($image,'/')+1),substr($image,strpos($image,'-gallery-')+9);
在PHP
這樣做:
function renameURL($originalUrl){
$array1 = explode("/", $originalUrl);
$lastPart = $array1[count($array1)-1];//Get only the name of the image
$array2 = explode("-", $lastPart);
$newLastPart = implode("-", array_slice($array2, 2));//Delete the first two parts (2207 & gallery)
$array1[count($array1)-1] = $newLastPart;//Concatenate the url and the image name
return implode("/", $array1);//return the new url
}
//Using the function :
$url = renameURL($url);
function get_numerics ($str) {
preg_match_all('/\d+/', $str, $matches);
return $matches[0];
}
$one = 'http://img.pass.com:7710/img.pass.com/img-1/2207-gallery-25171-content_gallery-1428380843.jpg';
$pos1 = strpos($one, get_numerics($one)[3]);
$pos2 = strrpos($one, '/')+1;
echo ( (substr($one, 0, $pos2).substr($one, $pos1)) );
看到它可以幫助您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.