简体   繁体   中英

How to get an exact value from a website using php DOM and save it in a database?

I want to get the span id "CPH1_lblCurrent" from the url and save it in the database. here is the code that i tried by seeing some examples.

<?php
$file = $DOCUMENT_ROOT. "http://www.mypetrolprice.com/2/Petrol-price-in-Delhi";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);

$xpath = new DOMXpath($doc);

$elements = $xpath->query('//span[@id="CPH1_lblCurrent"]');
if (!is_null($elements)) {
foreach ($elements as $element) {


$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
?>

This shows me the following.

Current Delhi Petrol Price = 67.12 Rs/Ltr

but i want only the value 67.12.

Can somebody help me.

try to use this simple regex for getting nubmer

.*= ([\d.]+) .*

preg_match

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM