당신은 그것에 대해 PHP's DOM module를 사용할 수 있습니다. DOMDocument :: loadhtmlfile()을 사용하여 페이지를 읽고 DOMXPath 객체를 만들고 class = "page-numbers"속성을 갖는 문서 내의 모든 범위 요소를 쿼리합니다.
(편집 : 죄송합니다, 그것은하지 당신이 찾고있는 무엇을, 두 번째 코드 참조)
$html = '<html><head><title>:::</title></head><body>
<div class="pager">
<span class="page-numbers current">1</span>
<a href="/users?page=2" title="go to page 2"><span class="page-numbers">2</span></a>
<a href="/users?page=3" title="go to page 3"><span class="page-numbers">3</span></a>
<a href="/users?page=4" title="go to page 4"><span class="page-numbers">4</span></a>
<a href="/users?page=5" title="go to page 5"><span class="page-numbers">5</span></a>
<span class="page-numbers dots">…</span>
<a href="/users?page=15" title="go to page 15"><span class="page-numbers">15</span></a>
<a href="/users?page=2" title="go to page 2"><span class="page-numbers next"> next</span></a>
</div>
</body></html>';
$doc = new DOMDocument;
// since the content "is already here" we use loadhtml(content)
// instead of loadhtmlfile(url)
$doc->loadhtml($html);
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query('//span[@class="page-numbers"]');
echo 'there are ', $nodelist->length, ' span elements having class="page-numbers"';
편집 : 수행이
<a href="/users?page=15" title="go to page 15"><span class="page-numbers">15</span></a>
(두 번째 마지막 a
요소) 항상 점 마지막 페이지로, 즉이 링크에 찾고있는 값이 포함되어 있습니까?
그런 다음 두 번째이지만 마지막으로 a
요소를 선택하고 그 하위 요소 인 span
요소를 선택하는 XPath 식을 사용할 수 있습니다.
//div[@class="pager"] <- select each <div> where the attribute class equals "pager"
//div[@class="pager"]/a <- select each <a> that is a direct child of the pager div
//div[@class="pager"]/a[position()=last()-1] <- select the <a> that is second but last
//div[@class="pager"]/a[position()=last()-1]/span <- select the direct child <span> of that second but last <a> element in the pager <div>
는 직접적인 함수 나 그렇게 쉬운 방법이 없습니다
$doc->loadhtml($html);
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query('//div[@class="pager"]/a[position()=last()-1]/span');
if (0 < $nodelist->length) {
echo $nodelist->item(0)->nodeValue;
}
else {
echo 'not found';
}
멋진 - 그것은 –
안녕 내가 이것을 시도했지만 제로 기능 getusers ($의 userurl) { $ 반환에 내가 기대 덕분에 doc = 새 DOMDocument; $ doc-> loadhtml ($ userurl); $ xpath = 새 DOMXPath ($ doc); $ nodelist = $ xpath-> query ('// span [@ 클래스 = "페이지 번호"]'); print_r ($ nodelist); 'there are', $ nodelist-> length, 'span 요소가 class = "page-numbers"'를 갖습니다. } URL은 http://ask.recipelabs.com/users –
입니다. URL을 전달하면 loadhtmlFILE()이 필요하고 loadhtml()은 필요하지 않습니다. – VolkerK