【问题标题】:PHP - Regular expression to extract all paths to the images from within the CSS filePHP - 从 CSS 文件中提取图像的所有路径的正则表达式
【发布时间】:2012-06-28 06:59:43
【问题描述】:

我正在尝试查找正则表达式,它将从 css 文件中提取所有路径。我已经搜索过,我能找到的只有这两个帖子,实际上似乎不起作用:

PHP - extract image paths from css

Why doesn't this PHP regular expression extract the url from the css value?

我想要得到的是这样的数组:

array(0 => 'image/header.gif', 1 => 'image/footer.png') etc.

知道提取它的正则表达式是什么 - 请记住,字符之间可能没有空格,或者 url() 的左括号和右括号之间也可能有额外的空格.

我正在使用以下内容来处理 css 并提取路径(但显然我没有正确的表达式来按照我想要的方式进行操作):

$images = array();                  
if (preg_match_all($regex, $cssContent, $matches)) {
    $images = $matches[1];
}

【问题讨论】:

  • 您的 css 文件中有示例吗?
  • 嗨 Mellamokb - 任何 css 文件都可以 - 我正在尝试不同的网站和不同的 css 文件。

标签: php css regex background-image


【解决方案1】:

整理好了。对于那些有兴趣了解的人 - 这是从 css 文件中提取所有图像路径的表达式:

'/url\(\s*[\'"]?(\S*\.(?:jpe?g|gif|png))[\'"]?\s*\)[^;}]*?/i'

【讨论】:

    【解决方案2】:

    我想是这样的:

    /url\s*\(\s*['"]([^'"]+)['"]\s*\)/
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-14
      • 2012-03-10
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 2013-06-03
      相关资源
      最近更新 更多