php highlight

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    php highlight

    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 Code:
    <?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:
    PHP Code:
    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:
    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'); 
    my new highlighter: (will pass html validation)
    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: .*?">)&lt;\?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*?)&lt;\?\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: .*?">\?&gt;\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 \/>\?&gt;\t*?\r*?\n*?\s*?<\/span>\t*?\r*?\n*?\s*?<\/code>/msi\', \'</span></code>\', $matches[1]);
                    $matches[1] = preg_replace(\'/(<span style="color: .*?">\?&gt;\t*?\r*?\n*?\s*?<br \/>\t*?\r*?\n*?\s*?<\/span>)\?&gt;(\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:
    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);
        }
    you can use the function like so:
    PHP Code:
    echo php_highlighter($data); 
    all thoughts and comments are welcome.
    ​enjoy.
    Last edited by Ghost; 05.01.16, 19:26.
    <?php
    include ('Ghost');
    if ($Post == true) {
    echo '

    sigpic
    alt='coding-talk.com!!' />';
    echo 'Sharing Is Caring!';
    } else {
    echo '

    alt='the username GHOST has been comprimised!' />';
    echo 'OMG SOMEBODY HELP ME!!';
    }
    ?>

    #2
    What's wrong with http://php.net/manual/en/function.highlight-string.php alone?
    Mobile chat, iphone chat, android chat, chat, rooms http://www.aiochat.com

    Comment


      #3
      Originally posted by kevk3v View Post
      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 '

      sigpic
      alt='coding-talk.com!!' />';
      echo 'Sharing Is Caring!';
      } else {
      echo '

      alt='the username GHOST has been comprimised!' />';
      echo 'OMG SOMEBODY HELP ME!!';
      }
      ?>

      Comment


        #4
        New and improved:
        PHP Code:
        <?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(\'/&lt;\?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.
        Last edited by Ghost; 15.01.16, 20:43.
        <?php
        include ('Ghost');
        if ($Post == true) {
        echo '

        sigpic
        alt='coding-talk.com!!' />';
        echo 'Sharing Is Caring!';
        } else {
        echo '

        alt='the username GHOST has been comprimised!' />';
        echo 'OMG SOMEBODY HELP ME!!';
        }
        ?>

        Comment


          #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
          ------------------

          Comment


            #6
            Originally posted by spiderwebs View Post
            Function declaration is much better practice
            you mean something like?
            PHP Code:
            if(!function_exists('php_highlighter')):
                function 
            php_highlighter($data) {
                    [..]
                }
            endif; 
            <?php
            include ('Ghost');
            if ($Post == true) {
            echo '

            sigpic
            alt='coding-talk.com!!' />';
            echo 'Sharing Is Caring!';
            } else {
            echo '

            alt='the username GHOST has been comprimised!' />';
            echo 'OMG SOMEBODY HELP ME!!';
            }
            ?>

            Comment


              #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
              ------------------

              Comment

              Working...
              X