Results 1 to 9 of 9

Thread: Detecting PHP/HTML/CSS/JAVA scripts and show them highlighted??

  1. #1
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Question Detecting PHP/HTML/CSS/JAVA scripts and show them highlighted??

    Hi,

    I have problems in my messages OUTPUT area. I want to detect script in my
    $zinute variable and show those scripts highlighted in the output. It is easy or hard to do?

    Thanks in advance.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Quote Originally Posted by bluewalrus View Post
    Can you explain how to use it? because I have text variable $zinute...

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    PHP Code:
    <?php

    $zinute 
    '<?php

    $string = \'hello\'  

    // Syntax highlighted PHP code

    ?>'
    ;

    highlight_string($zinute);
    ?>
    Put that in a script and you'll see that it's highlighted differently depending on the code, so comments will be yellow, variables in blue and strings in red.

    It also outputs it in the format you see above (including line breaks)

  5. #5
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Good works... How I can style the highlighted php code, because now, it is in one line...

  6. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Is it entered on one line?

    Mine linebreaks everwhere I have a line.

    For an example:

    http://christophermacdonald.net/?id=develop&sec=simple

    Code of it:
    PHP Code:
    <p>PHP to determine browser and log it :</p>
    <div class="code_block">
    <?php
    highlight_string
    ('<?php
    //Credit or more info http://php.net/manual/en/function.get-browser.php 
    function browser_info($agent=null) {
      // Declare known browsers to look for
      $known = array(\'msie\', \'firefox\', \'safari\', \'webkit\', \'opera\', \'netscape\',
        \'konqueror\', \'gecko\');
      // Clean up agent and build regex that matches phrases for known browsers
      // (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor
      // version numbers.  E.g. "2.0.0.6" is parsed as simply "2.0"
      $agent = strtolower($agent ? $agent : $_SERVER[\'HTTP_USER_AGENT\']);
      $pattern = \'#(?<browser>\' . join(\'|\', $known) .
        \')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#\';
      // Find all phrases (or return empty array if none found)
      if (!preg_match_all($pattern, $agent, $matches)) return array();
      // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
      // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
      // in the UA).  That\'s usually the most correct.
      $i = count($matches[\'browser\'])-1;
      return array($matches[\'browser\'][$i] => $matches[\'version\'][$i]);
    }
    $ua = browser_info();

    if ($ua[\'firefox\']) { 
        $browser = "Firefox";
    }
    if ($ua[\'msie\']) { 
        $browser =  "IE";
    }
    if ($ua[\'opera\']) { 
        $browser =  "Opera";
    }
    if ($ua[\'safari\']) { 
        $browser =  "Safari";
    }
    $stat_file = "browser.xml";
    $stats = file_get_contents($stat_file);
    $stats_are = explode ("<" . $browser . ">",$stats); 
    $stats_are = explode ("</" . $browser . ">", $stats_are[1]); 
    $value = $stats_are[0] + 1;
    $patterns = array();
    $patterns[0] = \'/<\' . "$browser" . \'>(.*)\' ."<\/$browser>" . \'/\';
    $replacements = array();
    $replacements[0] = "<$browser>$value</$browser>";
    $output = preg_replace($patterns, $replacements, $stats);
    file_put_contents($stat_file, $output);
    ?>
    '
    );
    ?>
    </div>
    <p>Contents of <strong>browser.xml</strong>:</p>
    <div class="code_block">
    <?php
    highlight_string
    ('<?xml version="1.0" encoding="utf-8"?>
    <Browsers>
        <Firefox>0</Firefox>
        <Safari>0</Safari>
        <IE>0</IE>
        <Chrome>0</Chrome>
        <Opera>0</Opera>
    </Browsers>'
    );
    ?>
    </div>
    </div>
    Corrections to my coding/thoughts welcome.

  7. #7
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    maybe this problem is related to my other post: http://www.dynamicdrive.com/forums/s...ad.php?t=53712

  8. #8
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Posted there but I don't see the highlight string being used.
    Corrections to my coding/thoughts welcome.

  9. #9
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    I mention there that I use AJAX, that means I just send $zinute value to post.php it writes message to mysql... And then, on other script I see all text's from mysql...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •