2013-09-23 19 views
0

我有一個任務從網站列表中提取一些數字。所有這些數字都有相同數量的數字,如1234567890.從網站中提取所有有10位數字的數字的方法?

如何提取所有使用PHP的具有10位數的數字?

+0

http://php.net/preg_match –

+4

preg_match_all以 '/ \ d {10} /' –

+0

http://stackoverflow.com/questions/4685500/regular-expression-for-10-沒有任何特殊字符的數字號碼 – reikyoushin

回答

1

使用regexplook ahead and look behind表達式:

  • (?<!\d) - 通過數字沒有前綴
  • \d{10} - 10號
  • (?!\d) - 通過數字
  • 不後綴

而且隨着preg_match_all()適用:

$matches = array(); 
preg_match_all('~(?<!\\d)(\\d){10}(!?\\d)~', $html, $matches); 
foreach($matches[1] as $match){ 
    var_dump($match); 
} 
1
<? 
$sites = array(  
     'http://foo.bar/',   
     'http://blah.baz/'   
); 

foreach ($sites as $site) {   
     $ch = curl_init($site);   
     curl_setopt($ch, CURLOPT_HEADER, false);   
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   

     $res = curl_exec($ch);   

     if ($res === false) {   
       echo "Failed to download $site: " . curl_error($ch) . "\n";   
     } else {   
       if (preg_match_all('/\d{10}/', $res, $matches) !== false) {   
         echo "Found some numbers at $site\n";   
         foreach ($matches as $match) {   
           echo "Found number: " . $match[0] . "\n";   
         } 
       } 
     } 

     curl_close($ch);   
} 
?> 
+1

我可能誤解了這個問題...... –

相關問題