Instead of regex you can use Domdocument, a PHP API that works with XML and HTML, an example would look like this:
$conteudoDoHtml = '<a href="/page/page/categoria/page?page=2&publica=1" rel="next">2</a>';
$dom = new DOMDocument;
$dom->loadHTML($conteudoDoHtml);
$ancoras = $dom->getElementsByTagName("a");
foreach($ancoras as $elementos) {
echo $elementos->getAttribute('href'), '<hr>';
}
So you would just do a regex to extract the page
$conteudoDoHtml = '<a href="/page/page/categoria/page?page=2&publica=1" rel="next">2</a>';
$dom = new DOMDocument;
$dom->loadHTML($conteudoDoHtml);
$ancoras = $dom->getElementsByTagName("a");
foreach($ancoras as $elementos) {
$data = $elementos->getAttribute('href');
echo 'Conteudo de href:', $data, '<br>';
preg_match('#(&|&|\?)page=(\d+)#', $data, $match);
echo 'page=', $data[2], '<br>';
var_dump($match);//Pra visualizar melhor o resultado do preg_match
echo '<hr>';
}
If I understand the only problem with your regex and that it is returning the
rel="next"` together?
– Guilherme Nascimento
Good evening, I wonder if my answer helped you? If not, report might have had some doubt in the use of it.
– Guilherme Nascimento