PHP删除特定字符(文件扩展名)后的所有内容[重复]

问题描述 投票:0回答:2
我想删除一组特定字符(即文件类型/扩展名)之后的所有内容。我已经尝试了许多在网上找到的脚本,但是没有一个脚本能够真正满足我的需要,它们要么删除文件扩展名,要么保留后面的部分参数。

$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

php regex explode substr file-extension
2个回答
1
投票
也许这可以帮助您。

$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


0
投票
PHP的parse_url()和基本名称如何?

$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);

© www.soinside.com 2019 - 2024. All rights reserved.