好的,如果我的preg_match_all
在我的代码段下面找不到任何内容,我怎么能让它回复一条消息,说明无可用?我无法访问代码段中的网址。
段:
<?php
//Urls to scrape from.
$URLs = array();
$URLs[] = 'https://namemc.com/u/Zapdos';
$working = '';
//Curl scraper.
foreach($URLs as $URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$page = curl_exec($ch);
$text = strip_tags($page);
$accounts = array();
preg_match_all('/(\w+) \s+ is \s+ available/x',$text,$accounts);
foreach($accounts[0] as $account){
$working .= ''.$account.''. PHP_EOL . '';
}
}
//Put the scraped proxies into the new .txt file.
file_put_contents('accounts.txt', $working, FILE_APPEND);
?>
答案 0 :(得分:1)
检查如下:
$result = preg_match_all('/(\w+) \s+ is \s+ available/x',$text,$accounts);
if ($result===false) {
// preg_match_all didn't find anything
} else {
// do something with matches...
}
您可以在preg_match_all
找到更多信息