使用数据抓取进行测试。我刮的产量是百分之一。所以我基本上打了一个php echo“%”显示不止一次
echo "%<br>";
在实际数字输出是
echo $ret_[66];
但是有地方的百分比实际数量出庭以及一个问题,这是不可取的结束。这是输出:
%
-0.02%
而我想要得到的是刚刚-0.02%
显然,我做错了与PHP。我真的很感激任何反馈/解决方案。谢谢!
全码:
<?php
error_reporting(E_ALL^E_NOTICE^E_WARNING);
include_once "global.php";
$doc = new DOMDocument;
// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
$doc->loadHTMLFile('http://www.moneycontrol.com/markets/global-indices/');
$xpath = new DOMXPath($doc);
$query = "//div[@class='MT10']";
$entries = $xpath->query($query);
foreach ($entries as $entry) {
$result = trim($entry->textContent);
$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_[66];
echo "%<br>";
}
“出现在数字之前”和一个循环(“foreach”)表明其中一行可能对echo $ ret_ [66];没有任何回应。 –
我猜它与此有关? 'deleteBlankInArray'也许尝试:'if($ ret_ [66]){echo $ ret_ [66];回声“%
”; }' –
谢谢Jared,我认为你是对的。我实际上找到了一个更简单的解决方案,它只是在底部创建另一个php文件<?php echo“%
”; ?>我会调查你的解决方案,因为它可能更有效率。谢谢。 – Masteryogurt