2014-03-05 80 views
0

我想要做的是獲取HTML文件的頁數,我已經得到了我想要的部分,但我無法單獨調整數字。如何在PHP中將純文本轉換爲字符串?

<?php 
include_once 'simple_html_dom.php'; 
error_reporting(E_ERROR); 

$html = file_get_html('http://www.seccionamarilla.com.mx/resultados/hospitales/distrito-federal/1'); 
foreach($html->find('.paginas',0) as $e){ 

//echo $e->plaintext; 
$res = $e->plaintext; 
$text = (string)$res; 
echo $res."<br>"; 
echo $text."<br>"; 
echo substr($text, 10); 

} 

?> 

我正在使用黃頁作爲示例。這是我得到的結果。

Resultados (3892) Página 1 - 2 - 3 - 4 - 5 
Resultados (3892) Página 1 - 2 - 3 - 4 - 5 
Resultados (3892) Página 1 - 2 - 3 - 4 - 5 

我試過了substr(),但它對它沒有任何影響。我唯一想要的是數字3892.我也想擺脫輸出完成前的行。它通過循環並輸出4或5行和3後沒有打印任何內容。

回答

0
<?php 
include_once 'simple_html_dom.php'; 
error_reporting(E_ERROR); 
$html = file_get_html('http://www.seccionamarilla.com.mx/resultados/hospitales/distrito-federal/1'); 
$results=$html->find('div.folios div.paginas',0)->plaintext; 
preg_match('/\((\d+)\)/',$results, $matches); 
$pages=(int)$matches[1]; 
echo $pages; 
相關問題