I know most undefined index notices are the result of not initializing variables. However, what causes them in arrays and such?
I have a few that I cannot wrap my head around:
[E_NOTICE] Undefined offset: 1
On line 27
Line 27:
list($file,$extension) = explode('.', basename($path));
[E_NOTICE] Undefined offset: 1
On line 35
Line 35:
list($key,$val) = preg_split('/=/',$b);
[E_NOTICE] Undefined variable: result
On line 45
Line 45:
$result .= $cat_name['categories_name'];
This is the full code (all notices are coming from this script):
function callback($pagecontent) {
$pagecontent = preg_replace_callback("/(<[Aa][ \r\n\t]{1}[^>]*href[^=]*=[ '\"\n\r\t]*)([^ \"'>\r\n\t#]+)([^>]*>)/",'wrap_href',$pagecontent);
return $pagecontent;
}
function transform_uri($param) {
$uriparts = parse_url($param[2]);
$newquery='';
$scheme = $uriparts['scheme'].'://';
if (($scheme != 'http://') && ($scheme != 'https://')) return $param[1].$param[2].$param[3];
$host = $uriparts['host'];
if ($host != $_SERVER['SERVER_NAME'] && $host != $_SERVER['SERVER_ADDR']) return $param[1].$param[2].$param[3];
$path = $uriparts['path'];
list($file,$extension) = explode('.', basename($path));
if($extension != 'php') return $param[1].$param[2].$param[3];
$extension = ".html";
$path = rtrim(dirname($path),'/');
$query = isset( $uriparts['query'] ) ? $uriparts['query'] : '';
$anchor = isset( $uriparts['anchor'] ) ? $uriparts['anchor'] : '';
if ($a = explode('&',$query)){
foreach ($a as $b) {
list($key,$val) = preg_split('/=/',$b);
switch ($key) {
case 'cPath':
if(preg_match('/^[_0-9]*$/',$val)){
if($cat_arr = explode('_', $val)){
$count = false;
foreach($cat_arr as $value){
$cat_Q = tep_db_query("select c.categories_id, cd.categories_name from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . $value . "' and c.categories_id = cd.categories_id");
$cat_name = tep_db_fetch_array($cat_Q);
if(!$count){
$result .= $cat_name['categories_name'];
$count = true;
}
else{
$result .= '_' . $cat_name['categories_name'];
}
}
$cat = '/category/'. str_replace(' ' , '+' , $result);
}
else{
$cat = '/category/'.$val;
}
}
else{
$cat = '/category/'.$val;
}
break;
case 'language':
$lan = $val.'/'.$path;
break;
case 'products_id':
$name_Q = tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . $val . "'");
$pro = ($t = tep_db_fetch_array($name_Q)) ? '/product/' . str_replace(" ", "_" , $t['products_name']) : '/product/'.$val;
break;
case 'manufacturers_id':
$band_Q = tep_db_query("select manufacturers_name, manufacturers_type from " . TABLE_MANUFACTURERS . " where manufacturers_id = '" . $val . "'");
$man = ($t = tep_db_fetch_array($band_Q)) ? '/'.$t['manufacturers_type'].'/'.str_replace(" ", "_" , $t['manufacturers_name']) : $man = '/band/'.$val;
break;
case 'catid':
if(strstr($_SERVER["HTTP_USER_AGENT"],'Mozilla')) $newquery .= $key.'='.$val.'&';
break;
default:
if($newquery || $key) $newquery .= $key.'='.$val.'&';
}
}
}
if ($newquery) $newquery = '?'.rtrim($newquery,'&');
$path = '';
if(isset($man)) $path .= $man;
if(isset($cat)) $path .= $cat;
if(isset($pro)) $path .= $pro;
((isset($man) || isset($cat) || isset($pro))) ? $host .= '' :$host .= '/';
if($file == 'index' || $file == 'product_info'){
if((isset($man) || isset($cat) || isset($pro))) $file= '';
}
if(preg_match('/^reviews$/',$file)) $file = '/' . $file;
return $param[1].$scheme.$host.$file.$path.$extension.$newquery.$anchor.$param[3];
}
function wrap_href($param) {
return transform_uri($param);
}
ob_start("callback");