Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP从字符串中删除子字符串?_Php_String - Fatal编程技术网

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

如何使用PHP从字符串中删除子字符串?,php,string,Php,String,给定以下字符串 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/特定响应 有什么建议吗?如果您试图从所有字

给定以下字符串

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/特定响应


有什么建议吗?

如果您试图从所有字符串中删除的字符只有
-100x100
,为什么不使用
str\u replace

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

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

  $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 );
演示:

使用的模式非常基本:

/ 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 /表示模式的开始 -文字字符 \d+一个数字,1次或多次 x字面值x字符 \d+一个数字,1次或多次 /表示模式的结束
使用
-100x100.jpg
作为防弹解决方案。

您是否计划对子字符串的值进行硬编码?或者您想匹配任何-WIDTHxHEIGHT.ext形式的子字符串?您介意链接到您找到的Ruby和Python版本吗?这里使用的技术可能是相关的。@minitech-在opc中添加了一些链接,但他需要保留扩展名,因此添加“.jpg”作为替换值。说到底,这可能是一个多功能的解决方案,以防这些缩略图的大小发生变化 / 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
$url = str_replace("-100x100.jpg", '.jpg', $url);