$urls = array(
'http://www.example.com/images/image1.jpg',
'http://www.example.com/images/image2.png?arg=value',
'http://www.example.com/images/image3.jpg?foo=bar',
'http://www.example.com/images/image4.gif?v=1',
'http://www.example.com/images/image5.bmp?x=y',
'http://www.example.com/images/image6.tiff?werdfs=234234'
);
期望的结果:
http://www.example.com/images/image1.jpg http://www.example.com/images/image2.png http://www.example.com/images/image3.jpg http://www.example.com/images/image4.gif http://www.example.com/images/image5.bmp http://www.example.com/images/image6.tiff
$re = '/^.*(?:\.)[a-zA-Z]+/m';
$urls = array(
'http://www.example.com/images/image1.jpg',
'http://www.example.com/images/image2.png?arg=value',
'http://www.example.com/images/image3.jpg?foo=bar',
'http://www.example.com/images/image4.gif?v=1',
'http://www.example.com/images/image5.bmp?x=y',
'http://www.example.com/images/image6.tiff?werdfs=234234',
'asdasd'
);
foreach ($urls as $url) {
preg_match($re, $url, $matches);
if ($matches) {
echo $matches[0];
echo "\n";
}
}
输出
http://www.example.com/images/image1.jpg http://www.example.com/images/image2.png http://www.example.com/images/image3.jpg http://www.example.com/images/image4.gif http://www.example.com/images/image5.bmp http://www.example.com/images/image6.tiff
$inName = $urls[0]; // for example
$newName = parse_url($inName,PHP_URL_SCHEME)
. parse_url($inName,PHP_URL_HOST)
. parse_url($inName,PHP_URL_PATH)
. basename($inName);