如何使用 xpath 从内部 css 获取背景图像?
How to get background image from internal css using xpath?
我想要获取内部 css 中的背景图像 URL。我在 php
中使用 xpath
这里是html结构
<div id="plugin-title" class="with-banner">
<div class="vignette"></div>
<style type="text/css">
#plugin-title { width:772px; height:250px; background-size:772px 250px; background-image: url(//ps.w.org/jetpack/assets/banner-772x250.png?rev=1279667); }
</style>
<h2 itemprop="name">Jetpack by WordPress.com</h2>
</div>
如果您想要 background-image
的链接,则可以使用此 RegExp。
background-image.*?url\((.*?)\)
示例实现:
$html = <<<EOT
<div id="plugin-title" class="with-banner">
<div class="vignette"></div>
<style type="text/css">
#plugin-title { width:772px; height:250px; background-size:772px 250px; background-image: url(//ps.w.org/jetpack/assets/banner-772x250.png?rev=1279667); }
</style>
<h2 itemprop="name">Jetpack by WordPress.com</h2>
</div>
EOT;
preg_match_all('/background-image.*?url\((.*?)\)/mi', $html, $matches);
$matches
包含所有 background-image
网址。
我想要获取内部 css 中的背景图像 URL。我在 php
中使用 xpath这里是html结构
<div id="plugin-title" class="with-banner">
<div class="vignette"></div>
<style type="text/css">
#plugin-title { width:772px; height:250px; background-size:772px 250px; background-image: url(//ps.w.org/jetpack/assets/banner-772x250.png?rev=1279667); }
</style>
<h2 itemprop="name">Jetpack by WordPress.com</h2>
</div>
如果您想要 background-image
的链接,则可以使用此 RegExp。
background-image.*?url\((.*?)\)
示例实现:
$html = <<<EOT
<div id="plugin-title" class="with-banner">
<div class="vignette"></div>
<style type="text/css">
#plugin-title { width:772px; height:250px; background-size:772px 250px; background-image: url(//ps.w.org/jetpack/assets/banner-772x250.png?rev=1279667); }
</style>
<h2 itemprop="name">Jetpack by WordPress.com</h2>
</div>
EOT;
preg_match_all('/background-image.*?url\((.*?)\)/mi', $html, $matches);
$matches
包含所有 background-image
网址。