php - 如何使用 PHP 从字符串中删除子字符串?

标签 php string

给定以下字符串

http://thedude.com/05/simons-cat-and-frog-100x100.jpg

我想使用 substrtrim (或任何您认为更合适的方式)来返回此

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/

相关文章:

PHP MySQL - 一次更新多行

java - 如何创建字符串数组并将其链接到成绩数组

java - nextLine 方法给了我一个不正确的实现

string - 如何存储字符串矩阵并写入文件?

C#逐行读取文本文件并编辑特定行

php - 多语言网站问题

php - 如何使用post方法获取while循环中的提交按钮值

php - mysql脚本仅重新加载

C 在不知道字符数的情况下分配一个字符串

php - Jquery跨域登录与PHP session