Jump to content

preg_replace php code issue


doddsey_65

Recommended Posts

im working on a php syntax highlighter for a bbeditor. basically all text within code tags will be placed into the function parse_code().

 

It works fine until i add the code to instantiate a class within the tags. This is supposed to just be a string but it looks like the 'e' modifier of preg_replace is recognising the code as php and executing it.

 

Is there any way around this?

 

Heres the regex:

 

'|\[code=(.*?)\](.*?)\[\/code\]|sie',


'self::parse_code("\\1", "\\2")',

 

and the parse_code method

 

public static function parse_code($code, $content)
    {
        $words = explode(' ', $content);
        
        dump($words);
        
        $keywords = array(
            'php_words' => array(
                'function',
                'echo',
                'class',
                'new',
                ),
        );

        foreach($words as $key => $value)
        {
            foreach($keywords as $group => $array)
            {
                foreach($array as $keyword)
                {
                    $words[$key] = preg_replace('|'.$keyword.'|si', '<span class="'.$group.'">'.$keyword.'</span>', $words[$key]);
                }
            }
            
            $words[$key] = preg_replace("|\\'(.*?)\\'|si", '<span class="string">\'\\1\'</span>', $words[$key]);
        }

        $content = implode(' ', $words);
        
        $lines = explode("\n", $content);
        
        unset($lines[0]);
        
        for($i=1; $i<count($lines); $i++)
        {
            $lines[$i] = '<p class="line_number">'.$i.'. </p>'.$lines[$i];
        }
        
        $content = implode("\n", $lines);

        return stripslashes('<blockquote class="code"><div class="code_header"><p>'.$code.'</p></div><div class="code_content"><p>'.$content.'</p></div></blockquote>');
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.