我的任务是从网站列表中提取一些数字。所有这些数字都具有相同的位数,例如 1234567890。
如何使用 PHP 从特定 url 中提取所有具有 10 位数字的数字?
(?<!\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);
}
<?
$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);
}
?>