Example: Regular expressions
Demonstrates how to work with regex module based on PRCE and PHP compatible functions.
<?v1
require_once ("lib/wget.inc.v1");
function getStockValues ()
{
url = "https://www.onvista.de";
// Use WGet library to receive HTML website
website = wget (url, null, false);
if (website["errText"]!="") {
return "Error: ".website["errText"];
}
else {
// List of PCRE patterns
patternList = [
"Dow Jones" => '<span class="top-assets__price" data-push="15305651:last:1:1:Index"> (.*?) <\/span>',
"German DAX" => '<span class="top-assets__price" data-push="14207349:last:1:1:Index"> (.*?) <\/span>',
"Oil (WTI)" => '<span class="top-assets__price" data-push="31117979:last:1:1:Commodity"> (.*?) <\/span>',
"USDEUR" => '<span data-push="1390634:last:2:0:Currency">(.*?)<\/span>',
"Gold" => '<span class="top-assets__price" data-push="31117890:last:1:1:Commodity">(.*?)<\/span>'
];
stockList = array ();
foreach (patternList as name => pattern) {
matches = array ();
// Check the pattern
ret = preg_match_all (pattern, website["content"], matches);
if (ret) {
// Value found, remove . and replace , with .
stockList[name]=str_replace (array (".", ","), array ("", "."), matches[1][0]);
}
else {
stockList[name]="<not found, check pattern>";
}
}
return stockList;
}
return -1;
}
print_r (getStockValues ());
?>