给定以下字符串
http://thedude.com/05/simons-cat-and-frog-100x100.jpg
我想使用 substr
或 trim
(或任何您认为更合适的方式)来返回此
http://thedude.com/05/simons-cat-and-frog.jpg
即删除-100x100
。我需要的所有图像都会将其标记到文件名的末尾,紧邻扩展名之前。
似乎在 Ruby 和 Python 上对此有回应,但没有 PHP/特定于我的需求。
How to remove the left part of a string?
Remove n characters from a start of a string
Remove substring from the string
有什么建议吗?
最佳答案
如果您想匹配任何宽度/高度值:
$path = "http://thedude.com/05/simons-cat-and-frog-100x100.jpg";
// http://thedude.com/05/simons-cat-and-frog.jpg
echo preg_replace( "/-\d+x\d+/", "", $path );
演示:http://codepad.org/cnKum1kd
使用的模式非常基本:
/ Denotes the start of the pattern - Literal - character \d+ A digit, 1 or more times x Literal x character \d+ A digit, 1 or more times / Denotes the end of the pattern
关于php - 如何使用 PHP 从字符串中删除子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10771248/