doddsey_65 Posted September 1, 2011 Share Posted September 1, 2011 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 More sharing options...
btherl Posted September 2, 2011 Share Posted September 2, 2011 You might want to try preg_replace_callback() instead. Link to comment https://forums.phpfreaks.com/topic/246227-preg_replace-php-code-issue/#findComment-1264583 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.