我有一个 PHP 代码,当上传并在服务器中运行时会显示此错误:
PHP Parse error: syntax error, unexpected '[' in /var/www/demo1/alibaba.php on line 14
但是,当我尝试使用 localhost 运行它时,它没有错误。只有在上传到服务器时。
这是我的 PHP 代码:
<?php
$ch = curl_init('http://www.alibaba.com/Products');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$html = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$finder = new DOMXPath($dom);
$nodes = $finder->query('//h4[@class="sub-title"]');
$showDate = date("Y.m.d");
$total_A = 0;
foreach ($nodes as $node) {
$sub_title = trim(explode("\n", trim($node->nodeValue))[0]) . " : " ; //error here
$sub_no = (int) preg_replace("/[^0-9]/", '', trim(explode("\n", trim($node->nodeValue))[2]));
$total_A += $sub_no;
}
$alibaba = number_format($total_A, 0 , '.' , ',' );
echo $alibaba;
?>
可能是什么原因造成的?
更新:
if($nodes->length > 0) {
foreach($nodes as $tr) {
if($finder->evaluate('count(./td/a)', $tr) > 0) {
foreach($finder->query('./td/a[@class="cate_menu"]', $tr) as $row) {
$number = $finder->query('./following-sibling::text()', $row)->item(0)->nodeValue;
$pronumber = str_replace(['(', ')'], '', $number); //error
$c_productno = number_format( $pronumber , 0 , '.' , ',' );
$total_T += (int) $pronumber;
}
}
$tradeindia = number_format( $total_T , 0 , '.' , ',' );