Reputation: 15
I have a php file that reads a html documents on another website and stores the data in a variable called "$content"
$ch = curl_init("http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
what I can't figure out is the best way to get this string of text from the $content/html
<b>111 players online</b>
I was going to try and get the text inbetween the tags but it's a rather large html file and there's hundreds of tags
I then looked into using html DOM but couldn't figure out how to determine the elementId or tagName
any help would be much appreciated
oh and for anyone wondering the website Im trying to get the players online text for is -
view-source:http://www.pkhonor.net/
Upvotes: 0
Views: 45
Reputation: 6005
PHPs DOMDocument is what you'd want to use here, specifically the loadHTML method to parse the HTML into an object. You can then grab all of the <b>
elements into an array which you can then loop through:
$doc = new DOMDocument();
$doc->loadHTML($content);
$bolds = $doc->getElementsByTagName('b');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
echo $node->nodeValue;
}
}
DOMDocument::getElementsByTagName
Upvotes: 2