为什么我会收到通知:未定义的偏移量:65以及如何修复它

时间:2011-10-25 14:22:37

标签: php

我继续在第31行获取此Notice: Undefined offset: 65,即此行if($k[$c]) $p = preg_replace('/\b'.base_convert($c, 10, $a).'\b/', $k[$c], $p);。我该如何解决这个问题?

//function to get source html
function get_data($url) {
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,$url);
    curl_setopt($curl_handle,CURLOPT_TIMEOUT,10);
    curl_setopt($curl_handle,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');
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,10);
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
    $html = curl_exec($curl_handle);
    curl_close($curl_handle);

    return $html;
}

//function to extract string beteen 2 delimeters
function extract_unit($string, $start, $end) {
    $pos = stripos($string, $start);
    $str = substr($string, $pos);
    $str_two = substr($str, strlen($start));
    $second_pos = stripos($str_two, $end);
    $str_three = substr($str_two, 0, $second_pos);
    $unit = trim($str_three);

    return $unit;
}

//function to decode packer endcoding
function js_unpack($p,$a,$c,$k) {
  while ($c--)
    if($k[$c]) $p = preg_replace('/\b'.base_convert($c, 10, $a).'\b/', $k[$c], $p);

  return $p;
}

//url source;
$adcrunch = get_data("http://adcrun.ch/BuVE");

//gather js_packer arguments
$arg_p = extract_unit($adcrunch, 'return p}(\'', ';\',');
$arg_a = extract_unit($adcrunch, '});\',', ',');
$arg_c = extract_unit($adcrunch, ',10,', ',\'|a0x1');
$arg_k = extract_unit($adcrunch, ',\'|', '\'.split');

$p = $arg_p;
$a = $arg_a;
$c = $arg_c;
$k = explode('|', $arg_k);

echo js_unpack($p, $a, $c, $k);

3 个答案:

答案 0 :(得分:1)

未定义偏移意味着数组$ k

中没有索引65

你可以通过以下任何一种方式“避免”警告:

if(isset($k[c]))

if(array_key_exists($c, $k))

晒。

答案 1 :(得分:0)

试试这个

if(isset($k[$c]) && $k[$c])

$c可能是65,$k[65]不存在

答案 2 :(得分:0)

或者你使用

if(array_key_exists($c,$k)) ...