我在div内有一个带有链接的字符串。如何使用正确的正则表达式进行解析?
我需要获取此值的数组,如下所示:
[
"/media/filer_public/b6/49/b6491a4d-5c0d-4a0f-aa9c-b32ea39912c6/category-2.jpg",
"/media/filer_public/93/65/9365c3bc-8649-4d9d-932e-144f16ed535c/category-3.jpg"
]
基本HTML(示例):
<a href="/napolnye-pokrytiya/" class="category_cart">
<div class="category_cart__container">
<div style="background-image: url('/media/filer_public/b6/49/b6491a4d-5c0d-4a0f-aa9c-b32ea39912c6/category-2.jpg')" class="category_cart__thumbnail"></div>
<div class="category_cart__content">
<p class="category_cart__title">Напольные покрытия</p>
</div>
</div>
</a>
<a href="/oboi/" class="category_cart">
<div class="category_cart__container">
<div style="background-image: url('/media/filer_public/93/65/9365c3bc-8649-4d9d-932e-144f16ed535c/category-3.jpg')" class="category_cart__thumbnail"></div>
<div class="category_cart__content">
<p class="category_cart__title">Обои</p>
</div>
</div>
</a>
答案 0 :(得分:0)
您应该使用DOMDocument和DOMXPath或类似的东西,但是如果您想用regexp来完成,对于给定的html,这应该可以解决问题:
<?php
$html_code =
'<a href="/napolnye-pokrytiya/" class="category_cart">
<div class="category_cart__container">
<div style="background-image: url(\'/media/filer_public/b6/49/b6491a4d-5c0d-4a0f-aa9c-b32ea39912c6/category-2.jpg\')" class="category_cart__thumbnail"></div>
<div class="category_cart__content">
<p class="category_cart__title">Напольные покрытия</p>
</div>
</div>
</a>
<a href="/oboi/" class="category_cart">
<div class="category_cart__container">
<div style="background-image: url(\'/media/filer_public/93/65/9365c3bc-8649-4d9d-932e-144f16ed535c/category-3.jpg\')" class="category_cart__thumbnail"></div>
<div class="category_cart__content">
<p class="category_cart__title">Обои</p>
</div>
</div>
</a>';
//it will look for match between url(' and ')
preg_match_all('/url\(\'(.*?)\'\)/', $html_code, $matches_array);
echo '<pre>';
var_dump($matches_array);
echo '</pre>';
$your_array = array();
//matches including url(' and ') are stored in $matches_array[0], excluded in $matches_array[1] so
foreach($matches_array[1] as $match) {
$your_array[] = $match;
}
echo '<pre>';
var_dump($your_array);
echo '</pre>';
?>
输出:
array(2) {
[0]=>
array(2) {
[0]=>
string(84) "url('/media/filer_public/b6/49/b6491a4d-5c0d-4a0f-aa9c-b32ea39912c6/category-2.jpg')"
[1]=>
string(84) "url('/media/filer_public/93/65/9365c3bc-8649-4d9d-932e-144f16ed535c/category-3.jpg')"
}
[1]=>
array(2) {
[0]=>
string(77) "/media/filer_public/b6/49/b6491a4d-5c0d-4a0f-aa9c-b32ea39912c6/category-2.jpg"
[1]=>
string(77) "/media/filer_public/93/65/9365c3bc-8649-4d9d-932e-144f16ed535c/category-3.jpg"
}
}
array(2) {
[0]=>
string(77) "/media/filer_public/b6/49/b6491a4d-5c0d-4a0f-aa9c-b32ea39912c6/category-2.jpg"
[1]=>
string(77) "/media/filer_public/93/65/9365c3bc-8649-4d9d-932e-144f16ed535c/category-3.jpg"
}