php highlight

Started on the old forum in January 2016 · view the original in the archive · old links redirect here

6 replies · 176 views · started 10 years ago · 1 min read

3 participants
Ghost Senior · 366 posts #1

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:

<?php
echo 'php is cool';
?>

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:

echo 'php is cool';

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:

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');

my new highlighter: (will pass html validation)

<?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;
}

?>

codebox css:

.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);
    }

you can use the function like so:

echo php_highlighter($data);

all thoughts and comments are welcome. ​enjoy.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Ghost Senior · 366 posts #3

kevk3v wrote: What's wrong with http://php.net/manual/en/function.highlight-string.php alone?

highlight_string() only highlights a string if the "user" wraps their code in pre-wrapped php tags <?php echo $code; ?> with this version. the user can post either like <?php echo $code; ?> or simply like echo $code; and the results will still get highlighted.

with this version of highligting i was simply covering all bases. and making sure a highlighted string was produced no matter how the user was to use the function.

also with a slight mod to the code. you could add link backs (for functions) created in your code. pointing to php.net.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Ghost Senior · 366 posts #4

New and improved:

<?php
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(\'/<\?php\s*?<br \/>(.*?<\/span>)/msi\', \'$1\', $matches[1]);
            endif;
        return \'<div class="codebox">\'.$matches[1].\'</div>\';
    }') , $data);
return $data;
}
?>

other one was slightly buggy after extensive checks. this one now fixes old bug. and is smaller in code also. now only uses start of php tag <?php since doesnt need ending tag to use highlight_string. so now preg_replace only needs to search for the starting tag the function adds if user doesnt wrap code in tags before use.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
spiderwebs Developer · 193 posts #5

Function declaration is much better practice

E107 Security Team Leader Proudly Support AccountLab Plus Billing Software

Want your Apps to be Developed ??? mail us your requirement at info@csarlab.com

Ghost Senior · 366 posts #6

spiderwebs wrote: Function declaration is much better practice

you mean something like?

if(!function_exists('php_highlighter')):
    function php_highlighter($data) {
        [..]
    }
endif;
<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
spiderwebs Developer · 193 posts #7

Yes exactly

E107 Security Team Leader Proudly Support AccountLab Plus Billing Software

Want your Apps to be Developed ??? mail us your requirement at info@csarlab.com

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close