上传到服务器时PHP文件上的语法错误

时间:2014-10-23 00:45:49

标签: php web-scraping localhost syntax-error server

我有一个PHP代码,在上传并在服务器中运行时会显示此错误:

PHP Parse error:  syntax error, unexpected '[' in /var/www/demo1/alibaba.php on line 14

但是,当我上传到服务器时尝试使用localhost..only运行它时没有错误。

这是我的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 , '.' , ',' );

1 个答案:

答案 0 :(得分:3)

您正在尝试使用PHP 5.4+中提供的数组解除引用。您的生产版本显然是PHP 5.3或更早版本。

explode("\n", trim($node->nodeValue))[0] // <-- here
explode("\n", trim($node->nodeValue))[2] // <-- here, too

您需要将其分为两部分。一个用于获取由explode()创建的数组,另一个用于从该数组中获取第一个和第三个元素。

$parts = explode("\n", trim($node->nodeValue));
$sub_title = trim($parts[0]) . " : " ; 
$sub_no =  (int) preg_replace("/[^0-9]/", '', trim($parts[2]));