制作一个正则表达式,匹配一个包含url和图像扩展名的字符串


make a regex that matches a string which comprises a url and image extensions

我不擅长regex,话虽如此,我正在尝试制作一个regex,它将允许url的所有图片类型如下:

$regex = '~mydomain.ide.com/polopoly_fs/([a-z0-9''.''_''-]+(''.gif|''.png|''.jpe?g))~i'; 

//包括所有格式的

其目的是通过如下功能获取网络图片:

/**
 * Get the Images
 * @param unknown $html
 */
function getImages($html) {
  echo "Hello from getImages <br/>";
  $matches = array();
  $regex = '~http://mySite.ide.com/([a-z0-9''.''_''-]+(''.gif|''.png|''.jpe?g))~i'; //include all formats
  preg_match_all($regex2, $html, $matches);
  foreach ($matches[1] as $img) {
    saveImg($img);
  }
}

但我需要一个regex,它可以用以下结构的url来完成:

http://mydomain.ide.com/polopoly_fs/1.573651!imageManager/318890795.jpg

有人能帮忙吗?

感谢

这样的东西就足够了。

/(.*.com'/[a-z_-]+'/[0-9'.a-z!]+'/[a-z0-9_-]+'.(gif|jpg|jpeg|png))/i

以下是PHP代码

<?php
$s = "http://mydomain.ide.com/polopoly_fs/1.573651!imageManager/3188890795.jpg";
preg_match("/(.*.com'/[a-z_-]+'/[0-9'.a-z!]+'/[a-z0-9_-]+'.(gif|jpg|jpeg|png))/i", $s, $arrMatches);
echo print_r($arrMatches[0], true);

实时预览

  • 不区分大小写的匹配
  • 将只匹配.com tld,但可以进行调整以匹配更多

好了,现在有点清楚了,我可以帮更多的忙。

如果我能正确理解你想要的是在整个html中搜索完全符合保存条件的图像url,而不管它们的名称或位置。

function getImages($html) {
  echo "Hello from getImages <br/>";
  $matches = array();
  $regex = '~http://.*/[a-z0-9._-]+(?:'.gif|'.png|'.jpe?g)~i'; //include all formats
  preg_match_all($regex, $html, $matches);
  foreach ($matches as $img) {
    saveImg($img[0]);
  }
}

你不必在这里捕获任何东西,匹配就足够了,唯一需要转义的字符就是。以匹配.gif.png等中的小圆点

如果我误解了,请随时发表评论,我会调整