如何在preg_match_all中仅打印组2的值,而不使用数组和循环
$url = 'https://hentaifox.com/gallery/58091/';
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
preg_match_all('!<a href="\/tag\/(.*?)\/"><span class="badge tag">(.*?)<\/span><\/a>!', $result, $tags);
我只希望(。*?)结果在任何地方使用它
答案 0 :(得分:0)
不可能做那样的事情吗?
<div class="item-info">
<p class="title product-field" translate="no" data-track-info="
{"description":"title"}" data-force-track="">
<a href="https://www.kobo.com/us/en/ebook/added-youth"
class="">Added Youth</a>
</p>
<p class="subtitle product-field" translate="no" data-track-info="
{"description":"subtitle"}" data-force-track="">
<a href="https://www.kobo.com/us/en/ebook/added-youth" class="">All Natural Anti-Aging Program</a>
</p>
(我不是专家,所以也许您需要稍微更改一下数字...)
答案 1 :(得分:0)
$group2=$tags[1];
print_r($group2);
但是! Do not parse HTML with regex,在这种情况下,请改用正确的HTML解析工具
$domd = @DOMDocument::loadHTML($result);
$xp = new DOMXPath($domd);
foreach ($xp->query("//a[contains(@href,'/tag/')]/span[1]") as $tag) {
$tags[] = trim($tag->textContent);
}