php - 如何使用 php dom xpath 或正则表达式获取样式表 URL?

标签 php dom xpath

我正在构建我的自定义库以合并所有屏幕 css 样式表,但我不确定如何仅获取媒体类型 screen 的样式表。例如:

<!-- This should be fetched -->
<link href="http://www.domain.com/style.css" rel="stylesheet" type="text/css" />
<!-- This should be fetched -->
<link href="http://www.domain.com/ie.css" rel="stylesheet" type="text/css" />

<style type="text/css" media="all">
  <!-- This should be fetched -->
  @import url("http://static.php.net/www.php.net/styles/phpnet.css");
</style>

<style type="text/css" media="screen">
   <!-- This should be fetched -->
  @import url("http://static.php.net/www.php.net/styles/site.css");
</style>

<style type="text/css" media="print">
  <!-- This should NOT be fetched since it is media type print -->
  @import url("http://static.php.net/www.php.net/styles/print.css");
</style>

鉴于上面的字符串,我只想提取 hrefurl 值。我不知道该怎么做。虽然我确实尝试过:

preg_match_all("/(url\([\'\"]?)([^\"\'\)]+)([\"\']?\))/", $html, $matches);
print_r($matches);

但它不会返回它。

有没有用 php dom、xpath 或 regex 来实现的解决方案?

最佳答案

这是工作代码! 我也为您创建了一个键盘 pastebin:http://codepad.org/WQzcO3k3

<?php

$inputString = '<!-- This should be fetched -->
<link href="http://www.domain.com/style.css" rel="stylesheet" type="text/css" />
<!-- This should be fetched -->
<link href="http://www.domain.com/ie.css" rel="stylesheet" type="text/css" />

<style type="text/css" media="all">
  <!-- This should be fetched -->
  @import url("http://static.php.net/www.php.net/styles/phpnet.css");
</style>

<style type="text/css" media="screen">
   <!-- This should be fetched -->
  @import url("http://static.php.net/www.php.net/styles/site.css");
</style>

<style type="text/css" media="print">
  <!-- This should NOT be fetched since it is media type print -->
  @import url("http://static.php.net/www.php.net/styles/print.css");
</style>';
$outputUrls = array();

@$doc = new DOMDocument();
@$doc->loadHTML($inputString);
$xml = simplexml_import_dom($doc); // just to make xpath more simple

$linksOrStyles = $xml->xpath('//*[@rel="stylesheet" or @media="all" or @media="screen"]');     


//print_r($linksOrStyles);

foreach ($linksOrStyles as $linkOrStyleSimpleXMLElementObj)
{
    if ($linkOrStyleSimpleXMLElementObj->xpath('@href') != false) {
      $outputUrls[] = $linkOrStyleSimpleXMLElementObj['href'] . '';
    } else {
        //get the 'url' value.
        $httpStart = strpos($linkOrStyleSimpleXMLElementObj.'', 'http://');
        $httpEnd = strpos($linkOrStyleSimpleXMLElementObj.'', '"', $httpStart);
        $outputUrls[] = substr($linkOrStyleSimpleXMLElementObj.'', $httpStart, ($httpEnd - $httpStart));
        //NOTE:Use preg_match only to get URL. i had to use strpos here 
        //since codepad.org doesnt suport preg
        /*
        preg_match(
            "#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie",
            ' ' . $linkOrStyleSimpleXMLElementObj,
            $matches
        );
        print_r($matches);
        $outputUrls[] = $matches[0];
        */
    }
}

echo 'Output Url list: ';
print_r($outputUrls);

?>

关于php - 如何使用 php dom xpath 或正则表达式获取样式表 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14272535/

相关文章:

javascript - 单击特定行内的按钮时获取行值

c# - 在 C# 中使用 XPath 表达式读取 XML 文件

php - 在 Docker 上重启 apache

php - 在本地连接 phpMyAdmin 和 AppEngine 时遇到问题

jquery - 与 jQuery 的 .Closest 相反(顶部/最远?)

javascript - 如何将数据表中动态添加的行包含到 DOM 树中,以便它的行为与页面加载时加载的任何其他行一样?

php - 如何使用 PEAR PHP Http_Request2 库在 PUT 方法中发送数据

php - 我的 custom.css 文件显示不同的 ssl 和非 ssl url magento 2

html - Xpath - 如果其类包含一些文本,则获取 HTML 元素

xml - 在 boolean 值上测试 =""总是返回 true