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.finanznachrichten.de";
// Use WGet library to receive HTML website
website = wget (url, null, false, false, ["noCheckCert" => true]);
if (website["errText"]!="") {
return "Error: ".website["errText"];
}
else {
// List of PCRE patterns
patternList = [
"Dow Jones" => 'data-item="AMAP_US2605661048.AII" data-field="Rate">(.*?)<\/div>',
"NASDAQ 100" => 'data-item="AMAP_US6311011026.AII" data-field="Rate">(.*?)<\/div>',
"German DAX" => 'data-item="AMAP_DE0008469008.AII" data-field="Rate">(.*?)<\/div>',
"Oil (WTI)" => 'data-item="AMAP_XC0009677409.AIR" data-field="Rate">(.*?)<\/td>',
"USDEUR" => 'data-item="AMAP_EU0009652759.AID" data-field="Rate">(.*?)<\/td>',
"Gold" => 'data-item="AMAP_XC0009655157.AIR" data-field="Rate">(.*?)<\/td>'
];
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 ());
?>