preg_match_all regex

Having issues using regex to grab HTML contained in a certain span.
Trying to get it to get safeytrfyh is available! on NameMC.com to make a fast checker that will check a pre-specified list if usernames are available instead of constantly typing in the username and clicking check.

An example page you guys can use is https://namemc.com/u/safeytrfyh
Im using cURL for this:

<?php
//Urls to scrape from.
$URLs = array();
$URLs[] = 'https://namemc.com/u/safeytrfyh';
$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);
$accounts = array();
preg_match_all('#<div><span[^>]*>(.*?)</span></div>#',$page,$accounts);
foreach($accounts[0] as $account){
    $working .= ''.$account.''. PHP_EOL . '';
}
}

//Put the scraped check into the new .txt file.
file_put_contents('accounts.txt', $working, FILE_APPEND);
?>


Source: regex

Leave a Reply