function cut_str($str,$sign,$number){ $array=explode($sign, $str); $length=count($array); if($number<0){ $new_array=array_reverse($array); $abs_number=abs($number); if($abs_number>$length){ return 'error'; }else{ return $new_array[$abs_number-1]; } }else{ if($number>=$length){ return 'error'; }else{ return $array[$number]; } } }
$str='123/456/789/abc';
示例:
echo cut_str($str,'/',0); //输出 123
echo cut_str($str,'/',2); //输出 789
echo cut_str($str,'/',-1);//输出 abc
echo cut_str($str,'/',-3);//输出 456