By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

undefined offset:2

P: 5
hello all..

I am using luminous php syntax highlighter in my codeigniter project. It is working i.e. it highlights the codes. But an error also encountered --

A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 2
Filename: ci-syntax-highlight/highlight.php
Line Number: 30

Array ( )
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 2
Filename: ci-syntax-highlight/highlight.php
Line Number: 30

can anyone help me out???
Jan 5 '12 #1
Share this Question
Share on Google+
7 Replies


100+
P: 1,059
you are trying to access array, but the index you are using is not valid.

say, you have defined
Expand|Select|Wrap|Line Numbers
  1. $array[0]='asdf';
  2. $array[1]='**';
Now you are trying to access:
Expand|Select|Wrap|Line Numbers
  1. echo $array[2]; //but index 2 does not exists
  2.  
Jan 5 '12 #2

P: 5
thanx johny...

but i m not getting this point in my code....
here's the highlight.php -


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /* place in application/config/hooks.php:
  4. $hook['display_override'] = array(
  5.   'class' => 'highlight',
  6.   'function' => 'hook',
  7.   'filename' => 'highlight.php',
  8.   'filepath' => 'hooks/ci-syntax-highlight',
  9.   'params' => array()
  10. );
  11. */
  12. class highlight {
  13.  
  14.   public function highlight() {
  15.     if (!class_exists('luminous')) {
  16.       require_once dirname(__FILE__) . '/luminous/luminous.php';
  17.     }
  18.   }
  19.   private function hook_cb($matches) {
  20.     $meta = $matches[2];
  21.     $code = $matches[3];
  22.  
  23.     // parse the classes, don't worry about this if it's a [code] 
  24.     if (strlen($matches[0]) && $matches[0][0] === '<') {
  25.       // code in <...> tags are legit. html so we need to unescape whatever they
  26.       // had to escape
  27.       $code = htmlspecialchars_decode($code);
  28.       preg_match('/class=([\'"])(.*?)(\\1)/', $meta, $m);
  29.       print_r($m);
  30.       $classes = preg_split('/\s+/', $m[2]);
  31.       if (!in_array('highlight', $classes)) return $matches[0];
  32.     }
  33.     $language = 'plain';
  34.     if (preg_match('/lang(uage)?=(.*)/', $meta, $m)) {
  35.       $language = $m[2];
  36.       if (strlen($language) && ($language[0] === '"' || $language[0] === "'")) {
  37.         if (($pos = strpos($language, $language[0], 1)) !== false) {
  38.           $language = substr($language, 1, $pos-1);
  39.         }
  40.       }
  41.     }
  42.     return luminous::highlight($language, $code);
  43.   }
  44.  
  45.   public function hook($params = array()) {
  46.     $CI = & get_instance();
  47.     $output = $CI->output->get_output();
  48.     if (!isset($params['header']) || $params['header'] === true) {
  49.       if (isset($params['theme'])) 
  50.         luminous::set('theme', $params['theme']);
  51.       if (!function_exists('base_url')) {
  52.         $CI->load->helper('url');
  53.       }
  54.       luminous::set('relative-root', 
  55.         base_url() . 'application/hooks/ci-syntax-highlight/luminous/');
  56.       $head = luminous::head_html();
  57.       // insert the stylesheets
  58.       $output = preg_replace('%</head%i',
  59.         "$head\n" . '$0', $output, 1);
  60.     }
  61.     $exps = array(
  62.       //  .. 
  63.       "/
  64.         \[(code)(.*?)\][ \t]*(?:[\r\n]|\r\n)?
  65.         (.*?)
  66.         \s*
  67.         \[\/code\]
  68.       /xs",
  69.       // <pre> or <code>
  70.       "/
  71.         <(pre|code)(.*?)>[ \t]*(?:[\r\n]|\r\n)?
  72.         (.*?)
  73.         \s*
  74.         <\/\\1>
  75.       /xs");
  76.     foreach($exps as $e) {
  77.       $output = preg_replace_callback($e, array($this, 'hook_cb'), $output);
  78.     }
  79.     echo $output;
  80.   }
  81. }
Jan 5 '12 #3

P: 5
nd i m getting error in this line -

$classes = preg_split('/\s+/', $m[2]);
plz help me out...
Jan 5 '12 #4

Dormilich
Expert Mod 5K+
P: 8,639
seems like there is no second match. what does the print_r() give you?
Jan 5 '12 #5

P: 5
thanx Dormilich..

print_r output -

Expand|Select|Wrap|Line Numbers
  1. Array (
  2.  [0] => class='highlight'
  3.  [1] => '
  4.  [2] => highlight
  5.  [3] => ' 
  6. // some more repetitions of that
Jan 5 '12 #6

Dormilich
Expert Mod 5K+
P: 8,639
are you sure it’s line #28? the error doesn’t make sense in this context.
Jan 5 '12 #7

P: 5
yes , error comes in line -
$classes = preg_split('/\s+/', $m[2]);
Jan 5 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.