what trying create syntax highlighter.what need creating reg expression find , highlight punction marks want match exclude strings between characters "",'' , exclude strings have been commented or defined//,/**/,#. useing pattern
'/(!|%|\^|\*|\(|\)|\+|\=|\-|>|<|\?|,|\.|\:|\;|\'|&|\[|\]|\}|\{|~)(?=[^>]*(<|$))/'
this part seems work exclude stuff between tags looks incorrect , clueless on how structure pattern correctly.
(?=[^>]*(<|$))
here highlight class
class syntaxhighlight { public static function process($s,$lang,$raw = false) { $orig_s = $s; $regexp = array( //keywords working '/(?<!\w|\$|\%|\@|>)(and|or|xor|for|do|while|foreach|as|return|die|exit|if|then|else| elseif|new|delete|try|throw|catch|finally|class|function|string| array|object|resource|var|bool|boolean|int|integer|float|double| real|string|array|global|const|static|public|private|protected| published|extends|switch|true|false|null|void|this|self|struct| char|signed|unsigned|short|long|break|goto|static_cast|const_cast| limit|asc|desc|order|by|select|from|where)(?!\w|=")/iux' => '<span class="k">$1</span>', //$var, %var, @var '/(?<!\w)( (\$|\%|\@)(\->|\w)+ )(?!\w)/ix' => '<span class="v">$1</span>', // numbers (also hex) '/(?<!\w)( 0x[\da-f]+| \d+ )(?!\w)(?=[^"]*(<|"))/ix' => '<span class="n">$1</span>', //comments '/(\/\*.*?\*\/| \/\/.*?\n| \#.[^a-za-z0-9]+?\| \<\!\-\-[\s\s]+\-\-\>| (?<!\\\)\'(.*?)(?<!\\\)\' )/isx' => '<span class="c">$1</span>', //char strings '/\'(.+?)\'(?=[^>]*(<|$))/' => "<span class='cs'>'$1'</span>", //back quote strings '/\`(.+?)\`(?=[^>]*(<|$))/' => "<span class='bs'>`$1`</span>", //strings '/"(.+?)"(?=[^>]*(<|$))/' => '<span class="s">"$1"</span>', //punc '/(!|%|\^|\*|\(|\)|\+|\=|\-|>|<|\?|,|\.|\:|\;|\'|&|\[|\]|\}|\{|~)(?=[^>]*(<|$))/' => '<span class="p">$1</span>', ); $s = preg_replace( array_keys($regexp), array_values($regexp), $s); if($lang == "text" || $lang == "other") $s = $orig_s; if($raw == "false") $s = self::displaylinecount($s); return '<pre style = "background-color:white;margin:0auto;text-align:left;overflow:auto;">'.$s.'</pre>'; } private static function displaylinecount($s) { $i=1; $count = substr_count($s, "\n"); $s = "<span style = 'color:#dfecf2;'>".$i."\t</span>".$s; while($i <= $count) { ++$i; $s = preg_replace("/[\n]/","<span style = 'color:#dfecf2;'>".$i."\t</span>",$s,1); } return $s; } }
i getting syntax highligh database , calling function this.
if(isset($row['syntax']) && $row['syntax'] == $cat_ && isset($row['title']) && $row['title'] == $top_ && isset($row['id']) && $row['id'] == $id_) { $id_ = isset($row['id']) ? $row['id'] : ''; $top_ = isset($row['title']) ? $row['title'] : ''; $top_ = htmlspecialchars($top_); $auth_ = isset($row['author']) ? $row['author'] : ''; $date_ = isset($row['date']) ? $row['date'] : ''; $post_ = isset($row['paste']) ? $row['paste'] : ''; $newtext = htmlspecialchars($post_); echo syntaxhighlight::process($newtext,$cat_,$raw); }
i new useing reg expression patterns please forgive me , thank in advance.
Comments
Post a Comment