[英]How to remove part of a file's name in a url
I need to remove a substring from the start of filename urls. 我需要从文件名url的开头删除一个子字符串。
The substring that I need to remove is always a series of numbers then a hyphen then the word gallery
then another hyphen. 我需要删除的子字符串始终是一系列数字,然后是连字符,然后是字gallery
然后是另一个连字符。
eg 2207-gallery-
, 2208-gallery-
, 1245-gallery-
, etc. 例如2207-gallery-
, 2208-gallery-
, 1245-gallery-
等。
How can I change this: 我该如何更改:
http://img.pass.com:7710/img.pass.com/img-1/2207-gallery-25171-content_gallery-1428380843.jpg
to this: 对此:
http://img.pass.com:7710/img.pass.com/img-1/25171-content_gallery-1428380843.jpg
The substring to be replaced is always different. 要替换的子字符串始终是不同的。
This will match 1 or more digits then hyphen then "gallery" then hyphen: 这将匹配1个或多个数字,然后是连字符,然后是“ gallery”,然后是连字符:
/\d+-gallery-/
PHP Code: ( Demo ) 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);
Output: 输出:
http://img.pass.com:7710/img.pass.com/img-1/25171-content_gallery-1428380843.jpg
Here is your non-regex method: 这是您的非正则表达式方法:
echo substr($image,0,strrpos($image,'/')+1),substr($image,strpos($image,'-gallery-')+9);
on PHP
do this : 在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)) );
See it help you. 看到它可以帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.