note 68274 added to function.highlight-string
| From: | thomasetluegger_dot_de at osu1 dot php dot net | Date: | Thu, 20 Jul 2006 12:20:02 +0000 |
| Subject: | note 68274 added to function.highlight-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-114972@lists.php.net to get a copy of this message | ||
Thanks to peter at int8 dot com, your comment saved quite some time.
In my case even short tags didn't work right (PHP 5.0.5).
The following function offers subversion independent highlighting for PHP5. If you want to display
code snippets without tags or use css for code formating it might be a usefull starting point. It is
intented for small code fragments only and does not handle sources with multiple php blocks
correctly.
Beware, the <code> tag is stripped off.
<?php
/*
These classes are used for highlighting, happy css-ing:
.phpdefault { color:#0000BB; font-weight: bold;}
.phpkeyword { color:#007700; font-weight: bold;}
.phpstring { color:#DD0000; font-weight: normal;}
.phpcomment { color:#FF8000; font-weight: normal;}
*/
/**
* Highlights PHP-source snippets with and without php-tags, inserts class definitions on request
*
* Strips <code> and <span color:black>, removes empty spans
*
* @param string $source Source to highlight
* @param boolean $classes, true links source elements to classes
* @return string
*/
function php_highlight($source, $classes = false)
{
if (version_compare( phpversion(), "5.0.0", "<")) return "PHP 5
required";
$r1 = $r2 = '##';
// adds required PHP tags (at least with vers. 5.0.5 this is required)
if ( strpos($source,' ?>') === false ) // xml is not THAT important ;-)
{
$source = "<?php ".$source." ?>";
$r1 = '#<\?.*?(php)?.*? #s';
$r2 = '#\?>#s';
}
elseif (strpos($source,'<? ') !== false)
{
$r1 = '--';
$source = str_replace('<? ','<?php ',$source);
}
$source = highlight_string($source,true);
if ($r1 =='--') $source =
preg_replace('#(<\?.*?)(php)?(.*? )#s','\\1\\3',$source);
$source = preg_replace (array ( '/.*<code>\s*<span style="color:
#000000">/', //
'#</span>\s*</code>#',
// <code><span black>
$r1, $r2, // php tags
'/<span[^>]*><\/span>/' // empty spans
),'',$source);
if ($classes) $source = str_replace( array('style="color:
#0000BB"','style="color: #007700"',
'style="color:
#DD0000"','style="color: #FF8000"'),
array('class="phpdefault"','class="phpkeyword"',
'class="phpstring"','class="phpcomment"',),$source);
return $source;
}
echo '<p>Some tests:</p><p>';
echo php_highlight('<?php $test = new func("Text"); /* regular tags, use classes
*/ ?>',true),'<br />';
echo 'Inline code: ',php_highlight('<? $test = new func("Text"); /*
short tags */ ?>'),
' works without or ',php_highlight('define (\'WITH_CLASSES\',\'
too\'); /* no tags */',true),'<br />';
echo php_highlight('$test = new func("Text"); /* no tags, no classes
*/'),'<br />';
echo '</p><p>You are running PHP: '.phpversion().'</p>';
?>
hope it saves someones time, Tom
----
Server IP: 217.160.72.57
Probable Submitter: 82.83.83.145
----
X-Spam-Status: No, hits=4.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_30_40,HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.highlight-string.php
Edit -- http://master.php.net/note/edit/68274
Del: integrated -- http://master.php.net/note/delete/68274/integrated
Del: useless -- http://master.php.net/note/delete/68274/useless
Del: bad code -- http://master.php.net/note/delete/68274/bad+code
Del: spam -- http://master.php.net/note/delete/68274/spam
Del: non-english -- http://master.php.net/note/delete/68274/non-english
Del: in docs -- http://master.php.net/note/delete/68274/in+docs
Del: other reasons-- http://master.php.net/note/delete/68274
Reject -- http://master.php.net/note/reject/68274
Search -- http://master.php.net/manage/user-notes.php