gpt4 book ai didi

php - 如何使用 xpath 从内部 css 获取背景图像?

转载 作者:行者123 更新时间:2023-11-28 16:00:01 27 4
gpt4 key购买 nike

我想获取内部 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 url。

关于php - 如何使用 xpath 从内部 css 获取背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40319932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com