【问题标题】:php regex ignoring size from image urlphp正则表达式忽略图像url的大小
【发布时间】:2012-07-14 07:09:49
【问题描述】:

我如何在 PHP 中使用正则表达式来实现这一点

http://www.domain.com/path/to/image/ipsum_image.jpg

匹配这个并忽略大小部分

http://www.domain.com/path/to/image/ipsum_image-300x200.jpg

请记住,300x200 可以是任何值,从 4charsx4chars 到 1charx1char

【问题讨论】:

  • 您希望得到什么?说清楚点,你试过什么?
  • 图片类型也可以是png或gif吗?

标签: php regex


【解决方案1】:
$subject = 'http://www.domain.com/path/to/image/ipsum_image-300x200.jpg';
$result = preg_replace('&(-[0-9]{1,4}x[0-9]{1,4})&is', '', $subject); 
echo $result;

【讨论】:

    【解决方案2】:
    $str= "http://www.domain.com/path/to/image/ipsum_image-300x200.jpg";
    $regex= '/(\d{1,4})x(\d{1,4}).jpg$/i';
    preg_match($regex, $str, $match, PREG_OFFSET_CAPTURE, 3);
    print_r($match);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 2012-05-15
      相关资源
      最近更新 更多