undefined offset:2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Neha Parihar
    New Member
    • Jan 2012
    • 5

    undefined offset:2

    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???
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    you are trying to access array, but the index you are using is not valid.

    say, you have defined
    Code:
    $array[0]='asdf';
    $array[1]='**';
    Now you are trying to access:
    Code:
    echo $array[2]; //but index 2 does not exists

    Comment

    • Neha Parihar
      New Member
      • Jan 2012
      • 5

      #3
      thanx johny...

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


      Code:
      <?php
      
      /* place in application/config/hooks.php:
      $hook['display_override'] = array(
        'class' => 'highlight',
        'function' => 'hook',
        'filename' => 'highlight.php',
        'filepath' => 'hooks/ci-syntax-highlight',
        'params' => array()
      );
      */
      class highlight {
      
        public function highlight() {
          if (!class_exists('luminous')) {
            require_once dirname(__FILE__) . '/luminous/luminous.php';
          }
        }
        private function hook_cb($matches) {
          $meta = $matches[2];
          $code = $matches[3];
          
          // parse the classes, don't worry about this if it's a &#91;code] 
          if (strlen($matches[0]) && $matches[0][0] === '<') {
            // code in <...> tags are legit. html so we need to unescape whatever they
            // had to escape
            $code = htmlspecialchars_decode($code);
            preg_match('/class=([\'"])(.*?)(\\1)/', $meta, $m);
      	  print_r($m);
            $classes = preg_split('/\s+/', $m[2]);
            if (!in_array('highlight', $classes)) return $matches[0];
          }
          $language = 'plain';
          if (preg_match('/lang(uage)?=(.*)/', $meta, $m)) {
            $language = $m[2];
            if (strlen($language) && ($language[0] === '"' || $language[0] === "'")) {
              if (($pos = strpos($language, $language[0], 1)) !== false) {
                $language = substr($language, 1, $pos-1);
              }
            }
          }
          return luminous::highlight($language, $code);
        }
      
        public function hook($params = array()) {
          $CI = & get_instance();
          $output = $CI->output->get_output();
          if (!isset($params['header']) || $params['header'] === true) {
            if (isset($params['theme'])) 
              luminous::set('theme', $params['theme']);
            if (!function_exists('base_url')) {
              $CI->load->helper('url');
            }
            luminous::set('relative-root', 
              base_url() . 'application/hooks/ci-syntax-highlight/luminous/');
            $head = luminous::head_html();
            // insert the stylesheets
            $output = preg_replace('%</head%i',
              "$head\n" . '$0', $output, 1);
          }
          $exps = array(
            //  .. 
            "/
              \[(code)(.*?)\][ \t]*(?:[\r\n]|\r\n)?
              (.*?)
              \s*
              \[\/code\]
            /xs",
            // <pre> or <code>
            "/
              <(pre|code)(.*?)>[ \t]*(?:[\r\n]|\r\n)?
              (.*?)
              \s*
              <\/\\1>
            /xs");
          foreach($exps as $e) {
            $output = preg_replace_callback($e, array($this, 'hook_cb'), $output);
          }
          echo $output;
        }
      }
      Last edited by Dormilich; Jan 5 '12, 12:26 PM. Reason: please use [CODE] [/CODE] tags when posting code

      Comment

      • Neha Parihar
        New Member
        • Jan 2012
        • 5

        #4
        nd i m getting error in this line -

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

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          seems like there is no second match. what does the print_r() give you?

          Comment

          • Neha Parihar
            New Member
            • Jan 2012
            • 5

            #6
            thanx Dormilich..

            print_r output -

            Code:
            Array (
             [0] => class='highlight'
             [1] => '
             [2] => highlight
             [3] => ' 
            ) 
            // some more repetitions of that
            Last edited by Dormilich; Jan 5 '12, 02:11 PM. Reason: pretty formatting

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              are you sure it’s line #28? the error doesn’t make sense in this context.

              Comment

              • Neha Parihar
                New Member
                • Jan 2012
                • 5

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

                Comment

                Working...