trevorrww
trevorrww

Reputation: 35

scraping a specific html element with curl php

<?php

$url = 'http://url.com/usersvalue.html/';

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);


print ($result);


?>

I'm trying to make a script which only outputs a specific line from a website, and that line is html and its

"<td>users</td><td>100</td>"

. I'm trying output the value of "<td>100</td>" which is 100 at this point, each time the value changes on the $url website..

the html of the $url website:

<html><head></head><body style='font-family:Verdana'>
<h2>Status</h2><hr>
<table cellpadding='6' cellspacing='0' border='0'>
<tr bgcolor='#eeeeee'><th align='left'>Key</th><th align='left'>Value</th></tr>
<tr>
<td>users</td><td>100</td>
</tr>
<tr>
<td>uptime</td><td>00</td>
</tr>
<tr>
<td>zones</td><td>0</td>
</tr>
<tr>
<td>rooms</td><td>0</td>
</tr>
<tr>
<td>version</td><td>0</td>
</tr>
</table><hr>
</body></html>

Upvotes: 0

Views: 1421

Answers (1)

Quynh Nguyen
Quynh Nguyen

Reputation: 3009

You can use my source

function getStr($string,$start,$end){
    $str = explode($start,$string,2);
    $str = explode($end,$str[1],2);
    return $str[0];
}
echo getStr($result,'<td>users</td><td>','</td>');

OR

if (!preg_match('#<td>users</td><td>([0-9]+)</td>#', $result, $result_preg)) { 
    die('bad dsid'); 
}else{
    echo $result_preg[1];
}

Upvotes: 2

Related Questions