- array_values: documentation ( source)
- explode: documentation ( source)
- array_filter: documentation ( source)
- trim: documentation ( source)
<?php
$doc = new DOMDocument;
// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
$doc->loadHTMLFile('https://www.bloomberg.com/quote/USDJPY:CUR');
$xpath = new DOMXPath($doc);
$query = "//meta[@itemprop='priceChangePercent']";
$entries = $xpath->query($query);
foreach ($entries as $entry) {
$result = trim($entry->getAttribute('content'));
$ret_ = explode(' ', $result);
//make sure every element in the array don't start or end with blank
foreach ($ret_ as $key=>$val){
$ret_[$key]=trim($val);
}
//delete the empty element and the element is blank "\n" "\r" "\t"
//I modify this line
$ret_ = array_values(array_filter($ret_,deleteBlankInArray));
//echo the last element
echo $ret_[0];
}
?>