0

How to print only the value of group 2 in preg_match_all without the array and without loops

$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);

I only want (.*?) result to use it any where

  • Possible duplicate of [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – hanshenrik Apr 13 '19 at 22:00
  • 2
    Do not switch `CURLOPT_SSL_VERIFYPEER` off. – Dharman Apr 13 '19 at 22:00

2 Answers2

0

Isn't it possible to do something like that ?

$matches = preg_match_all('!<a href="\/tag\/(.*?)\/"><span class="badge tag">(.*?)<\/span><\/a>!', $result, $tags);
$group_2 = $matches[2][0][0];

(I'm no expert, so maybe you will have to change the numbers a little bit...)

Charles.C
  • 345
  • 1
  • 3
  • 12
0
$group2=$tags[1];
print_r($group2);

but! Do not parse HTML with regex, use proper HTML parsing tools instead, in this case

$domd = @DOMDocument::loadHTML($result);
$xp = new DOMXPath($domd);
foreach ($xp->query("//a[contains(@href,'/tag/')]/span[1]") as $tag) {
    $tags[] = trim($tag->textContent);
}
hanshenrik
  • 19,904
  • 4
  • 43
  • 89