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
https://forums.phpfreaks.com/topic/246227-preg_replace-php-code-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.