decided to make a new highlighting function for my up and coming script today.
highlighter will check if user has wrapped code in <?php ?> tags or not.
if user has wrapped in <?php ?> tags then it will run through the normal process
example:
	
if user didn't wrap in <?php ?> tags then it will wrap up code in tags run through process
then it will go through the code and re-strip the tags which is a harder process than would think.
example:
	
it goes through a process of reversing the created html that highlight_string creates,
getting/deleting the <?php ?> tags we created earlier to run highlight_string then starts
building the code back up to html verification standards (will pass html validation)
you can also use it with ini_set for changing colors of highlighter and not have to modify code in anyway
since i used regular expressions to solve it. it doesn't look for any particular color hex value.
example:
	
my new highlighter: (will pass html validation)
	
codebox css:
	you can use the function like so:
	
all thoughts and comments are welcome.
​enjoy.
					highlighter will check if user has wrapped code in <?php ?> tags or not.
if user has wrapped in <?php ?> tags then it will run through the normal process
example:
PHP Code:
	
	
<?php
echo 'php is cool';
?>
then it will go through the code and re-strip the tags which is a harder process than would think.
example:
PHP Code:
	
	
echo 'php is cool'; 
getting/deleting the <?php ?> tags we created earlier to run highlight_string then starts
building the code back up to html verification standards (will pass html validation)
you can also use it with ini_set for changing colors of highlighter and not have to modify code in anyway
since i used regular expressions to solve it. it doesn't look for any particular color hex value.
example:
PHP Code:
	
	
ini_set('highlight.default', '#3DD909');
ini_set('highlight.keyword','#04D8F3');
ini_set('highlight.string', '#E5CC0B');
ini_set('highlight.comment', '#FFAB01');
ini_set('highlight.html', '#000000'); 
PHP Code:
	
	
<?php
/** php highlighter */
function php_highlighter($data) {
    $data = preg_replace_callback('/\[PHP\](.*?)\[\/PHP\]/msi', create_function('$matches', '{
        $matches[1] = html_entity_decode($matches[1], ENT_QUOTES, \'UTF-8\');
            if(preg_match(\'/^(<\?|<\?php)\s*?.*?\s*?^\?>\s*?$/msi\', $matches[1], $new_matches)):
                $matches[1] = highlight_string($new_matches[0], 1);
            else:
                $matches[1] = highlight_string(\'<?php\'.$matches[1].\'?>\', 1);
                $matches[1] = preg_replace(\'/(<span style="color: .*?">\t*?\r*?\n*?\s*?<span style="color: .*?">)<\?php\t*?\r*?\n*?\s*?<br \/>(.*?\t*?\r*?\n*?\s*?<\/span>\t*?\r*?\n*?\s*?)/msi\', \'$1$2\', $matches[1]);
                $matches[1] = preg_replace(\'/(<span style="color: .*?">\t*?\r*?\n*?\s*?)<\?\t*?\r*?\n*?\s*?<br \/>(.*?\t*?\r*?\n*?\s*?<\/span>\t*?\r*?\n*?\s*?)/msi\', \'$1$2\', $matches[1]);
                $matches[1] = preg_replace(\'/<br \/>\t*?\r*?\n*?\s*?<\/span><span style="color: .*?">\?>\t*?\r*?\n*?\s*?<\/span>\t*?\r*?\n*?\s*?<\/span>\t*?\r*?\n*?\s*?<\/code>/msi\', \'</span></code>\', $matches[1]);
                $matches[1] = preg_replace(\'/<br \/>\?>\t*?\r*?\n*?\s*?<\/span>\t*?\r*?\n*?\s*?<\/code>/msi\', \'</span></code>\', $matches[1]);
                $matches[1] = preg_replace(\'/(<span style="color: .*?">\?>\t*?\r*?\n*?\s*?<br \/>\t*?\r*?\n*?\s*?<\/span>)\?>(\t*?\r*?\n*?\s*?<\/span>\t*?\r*?\n*?\s*?<\/code>)/msi\', \'$1$2\', $matches[1]);
                $matches[1] = preg_replace(\'/<code>(.*?)<\/code>/msi\', \'<code>$1</span></code>\', $matches[1]);
            endif;
        return \'<div class="codebox">\'.$matches[1].\'</div>\';
    }') , $data);
return $data;
}
?>
Code:
	
	.codebox {
    text-align:left;
    word-break:break-all;
    overflow:auto;
    background:#2D2D2D;
    font-size:12px;
    min-width:1px;
    max-width:90%;
    min-height:1px;
    max-height:300px;
    margin:10px;
    padding:3px;
    border-radius:6px;
    box-shadow:inset 0 1px 1px rgba(0,0,0,0.39),0 1px 0 rgba(255,255,255,0.16);
    }
PHP Code:
	
	
echo php_highlighter($data); 
​enjoy.


Comment