在php中抓取特定的数组元素

时间:2012-11-25 19:01:38

标签: php arrays explode

我想从CURl输出中获取特定的数组元素。所以,我正在尝试创建一个脚本。 这是:

<?php
$url = "example.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$buffer = curl_exec($ch);
$explode=explode("name",$buffer);
$a=($explode[89],$explode[18],$explode[58],$explode[36],$explode[49],$explode[58],$explode[68],$explode[78],$explode[88],$explode[98],$explode[108],$explode[118],$explode[158],$explode[138],$explode[148],$explode[158],$explode[168],$explode[178],$explode[188],$explode[198],$explode[508],$explode[518],$explode[558],$explode[538],$explode[548]);
$p=explode(",",$a);
foreach($p as $b){
     $c=explode("name-finish",$b);
     echo ($c[0]);
}   
?>

但它正在显示

  

解析错误:第23行的语法错误,意外','

(这是$ a的行)

我的错误是什么?

2 个答案:

答案 0 :(得分:2)

尝试:

$a=array($explode[89],$explode[18],$explode[58],$explode[36],$explode[49],$explode[58],$explode[68],$explode[78],$explode[88],$explode[98],$explode[108],$explode[118],$explode[158],$explode[138],$explode[148],$explode[158],$explode[168],$explode[178],$explode[188],$explode[198],$explode[508],$explode[518],$explode[558],$explode[538],$explode[548]);

然后删除该行:$p=explode(",",$a);

答案 1 :(得分:1)

删除$ a = ..和$ p ..行并替换为:

$explode=explode("name",$buffer);
$a[]=$explode[89];
$a[]=$explode[58];

...

然后

foreach($a as $b){...

OR

foreach (array($explode[89],$explode[58] ...) as $b){...

如果你不打算再次使用该阵列,那就更好了