Log in

View Full Version : Detecting PHP/HTML/CSS/JAVA scripts and show them highlighted??



auriaks
04-03-2010, 12:49 PM
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.

bluewalrus
04-03-2010, 03:58 PM
http://php.net/manual/en/function.highlight-string.php

That will highlight php.

auriaks
04-03-2010, 04:18 PM
http://php.net/manual/en/function.highlight-string.php

That will highlight php.

Can you explain how to use it? because I have text variable $zinute...

Schmoopy
04-03-2010, 04:30 PM
<?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)

auriaks
04-03-2010, 04:51 PM
Good :D works... How I can style the highlighted php code, because now, it is in one line...

bluewalrus
04-03-2010, 08:40 PM
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:


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

auriaks
04-03-2010, 09:25 PM
maybe this problem is related to my other post: http://www.dynamicdrive.com/forums/showthread.php?t=53712

bluewalrus
04-03-2010, 09:46 PM
Posted there but I don't see the highlight string being used.

auriaks
04-04-2010, 07:09 PM
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...