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

pep*_*epe 12 php string

给出以下字符串

http://thedude.com/05/simons-cat-and-frog-100x100.jpg
Run Code Online (Sandbox Code Playgroud)

我想使用substrtrim(或任何你认为更合适的东西)来返回它

http://thedude.com/05/simons-cat-and-frog.jpg
Run Code Online (Sandbox Code Playgroud)

也就是说,删除-100x100.我需要的所有图像都会在扩展名之前标记到文件名的末尾.

似乎有关于SO和Ruby和Python的响应,但不是PHP /特定于我的需求.

如何删除字符串的左侧部分?

从字符串的开头删除n个字符

从字符串中删除子字符串

有什么建议?

Sam*_*son 25

如果要匹配任何宽度/高度值:

  $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 );
Run Code Online (Sandbox Code Playgroud)

演示: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

  • 谢谢JS - 我知道正在出现一个正则表达式! (4认同)

Woj*_*ekT 16

$url = "http://thedude.com/05/simons-cat-and-frog-100x100.jpg";
$new_url = str_replace("-100x100","",$url);
Run Code Online (Sandbox Code Playgroud)


flo*_*ree 9

$url = str_replace("-100x100.jpg", '.jpg', $url);
Run Code Online (Sandbox Code Playgroud)

使用-100x100.jpg防弹解决方案.