Jump to content

[SOLVED] preg_match_all() - question


atticus

Recommended Posts

Hi all. I am trying to parse HTML and grab a title from the text. Right now, I am getting just a blank page (error reporting is turned on)

 

Here is the problem code:

 

$regex = '~<span class="main-title">(?)</span>~';
preg_match_all($regex,$html,$match);
foreach ($match[1] as $digit) {
echo $digit;
}

 

Here is the complete function:

 

function site($URL) {
$target_url = "$URL";
$userAgent = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12\r\n';

// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);
if (!$html) {
echo "
cURL error number:" .curl_errno($ch);
echo "
cURL error:" . curl_error($ch);
exit;
}

// parse the html into a DOMDocument
// $dom = new DOMDocument();
// @$dom->loadHTML($html);

//echo $html;

$regex = '~<span class="main-title">(?)</span>~';
preg_match_all($regex,$html,$match);
foreach ($match[1] as $digit) {
echo $digit;
}
}

Link to comment
https://forums.phpfreaks.com/topic/181166-solved-preg_match_all-question/
Share on other sites

Hi all. I am trying to parse HTML and grab a title from the text. Right now, I am getting just a blank page (error reporting is turned on)

 

Here is the problem code:

 

$regex = '~<span class="main-title">(?)</span>~';
preg_match_all($regex,$html,$match);
foreach ($match[1] as $digit) {
echo $digit;
}

 

 

Should be:

 

<span class="main-title">(.+)</span>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.