如何使用 phpQuery 获取下一页 url?

How to get next page url with phpQuery?

我有自定义 HTML 分页模板:

<ul class="number-list">       
    <li><a href="/article/" class="page-number js-page-filter " data-page="1">1</a></li>
    // Current page with class [.red]
    <li><a href="/article/?page=2" class="page-number js-page-filter red" data-page="2">2</a></li>
    <li><a href="/article/?page=3" class="page-number js-page-filter " data-page="3">3</a></li>
    <li><a href="/article/?page=4" class="page-number js-page-filter " data-page="4">4</a></li>
    <li><a href="/article/?page=5" class="page-number js-page-filter " data-page="5">5</a></li>
    <li><span class="page-number">...</span></li>
    <li><a href="/article/?page=31" class="page-number js-page-filter " data-page="31">31</a></li>
    <li><a href="/article/?page=32" class="page-number js-page-filter " data-page="32">32</a></li>
</ul>

我可以使用此代码获取当前页面 url:

$current = $doc->find('.number-list li a.red')->attr('href');

在我的情况下,如何才能获得下一页 url?

我累了:

$next = $doc->find('.number-list li a.red')->next('a')->attr('href');
$next = $doc->find('.number-list li a.red')->next('li a')->attr('href');
$next = $doc->find('.number-list li a.red')->next('.page-number')->attr('href');
$next = $doc->find('li a.red')->next('li a')->attr('href');
$next = $doc->find('li a.red')->next('li .page-number')->attr('href');

安德烈亚斯。你可以获取所有 <li> 并检查它 <a> 是否有 class red。 这个元素将是你的当前元素,之后,你可以获得下一个元素。 希望对你有帮助。

$document = phpQuery::newDocument('
<ul class="number-list">       
    <li><a href="/article/" class="page-number js-page-filter " data-page="1">1</a></li>
    <li><a href="/article/?page=2" class="page-number js-page-filter red" data-page="2">2</a></li>
    <li><a href="/article/?page=3" class="page-number js-page-filter " data-page="3">3</a></li>
    <li><a href="/article/?page=4" class="page-number js-page-filter " data-page="4">4</a></li>
    <li><a href="/article/?page=5" class="page-number js-page-filter " data-page="5">5</a></li>
    <li><span class="page-number">...</span></li>
    <li><a href="/article/?page=31" class="page-number js-page-filter " data-page="31">31</a></li>
    <li><a href="/article/?page=32" class="page-number js-page-filter " data-page="32">32</a></li>
</ul>
');

$lis = $document->find('.number-list li');
foreach ($lis as $li)
{
    if(pq($li)->find('a')->hasClass('red'))
    {
       var_dump(pq($li)->next()->find('a')->attr('href')); // get the next li and it a tag
    }
}