【问题标题】:A way to extract all the numbers who have 10 digits from a web site? [closed]一种从网站中提取所有 10 位数字的方法? [关闭]
【发布时间】:2013-09-24 05:41:51
【问题描述】:

我的任务是从网站列表中提取一些数字。所有这些数字都有相同的位数,例如 1234567890。

如何使用 PHP 从特定 url 中提取所有 10 位数字?

【问题讨论】:

标签: php


【解决方案1】:

使用regexp 和否定look 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);
}

【讨论】:

    【解决方案2】:
    <?
    $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);        
    }
    ?>
    

    【讨论】:

    • 我可能误解了这个问题...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2013-03-05
    • 1970-01-01
    • 2013-08-19
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多