note 81257 added to function.highlight-string
| From: | Daniel at osu1 dot php dot net | Date: | Wed, 20 Feb 2008 10:49:01 +0000 |
| Subject: | note 81257 added to function.highlight-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136304@lists.php.net to get a copy of this message | ||
Well, Just a little something I wrote which highlights an HTML code...It'll be going through
many changes in the next few days.... until then =) enjoy
<?php
/*************************************\
CODE PANE 1.0 - SILVERWINGS - D. Suissa
\*************************************/
class HTMLcolorizer{
private $pointer = 0; //Cursor position.
private $content = null; //content of document.
private $colorized = null;
function __construct($content){
$this->content = $content;
}
function colorComment($position){
$buffer = "<<span class='HTMLComment'>";
for($position+=1;$position < strlen($this->content) && $this->content[$position]
!= ">" ;$position++){
$buffer.= $this->content[$position];
}
$buffer .= "</span>>";
$this->colorized .= $buffer;
return $position;
}
function colorTag($position){
$buffer = "<<span class='tagName'>";
$coloredTagName = false;
//As long as we're in the tag scope
for($position+=1;$position < strlen($this->content) && $this->content[$position]
!= ">" ;$position++){
if($this->content[$position] == " " && !$coloredTagName){
$coloredTagName = true;
$buffer.="</span>";
}else if($this->content[$position] != " " && $coloredTagName){
//Expect attribute
$attribute = "";
//While we're in the tag
for(;$position < strlen($this->content) && $this->content[$position] !=
">" ;$position++){
if($this->content[$position] != "="){
$attribute .= $this->content[$position];
}else{
$value="";
$buffer .= "<span
class='tagAttribute'>".$attribute."</span>=";
$attribute = ""; //initialize it
$inQuote = false;
$QuoteType = null;
for($position+=1;$position < strlen($this->content) &&
$this->content[$position] != ">" && $this->content[$position] != "
" ;$position++){
if($this->content[$position] == '"' || $this->content[$position] ==
"'"){
$inQuote = true;
$QuoteType = $this->content[$position];
$value.=$QuoteType;
//Read Until next quotation mark.
for($position+=1;$position < strlen($this->content) &&
$this->content[$position] != ">" && $this->content[$position] !=
$QuoteType ;$position++){
$value .= $this->content[$position];
}
$value.=$QuoteType;
}else{//No Quotation marks.
$value .= $this->content[$position];
}
}
$buffer .= "<span
class='tagValue'>".$value."</span>";
break;
}
}
if($attribute != ""){$buffer.="<span
class='tagAttribute'>".$attribute."</span>";}
}
if($this->content[$position] == ">" ){break;}else{$buffer.=
$this->content[$position];}
}
//In case there were no attributes.
if($this->content[$position] == ">" && !$coloredTagName){
$buffer.="</span>>";
$position++;
}
$this->colorized .= $buffer;
return --$position;
}
function colorize(){
$this->colorized="";
$inTag = false;
for($pointer = 0;$pointer<strlen($this->content);$pointer++){
$thisChar = $this->content[$pointer];
$nextChar = $this->content[$pointer+1];
if($thisChar == "<"){
if($nextChar == "!"){
$pointer = $this->colorComment($pointer);
}else if($nextChar == "?"){
//colorPHP();
}else{
$pointer = $this->colorTag($pointer);
}
}else{
$this->colorized .= $this->content[$pointer];
}
}
return $this->colorized;
}
}
$curDocName = $_REQUEST['doc'];
$docHandle = fopen($curDocName,"r");
$docStrContent = fread($docHandle,filesize($curDocName));
fclose($docHandle);
$HTMLinspector = new HTMLcolorizer($docStrContent);
$document = $HTMLinspector->colorize();
?>
<html>
<head>
<style type="text/css">
/**********************\
* MOZILLA FIREFOX STYLE
\**********************/
/*pre{font-family:Tahoma;font-size:px;}*/
.tagName{color:purple;}
.tagAttribute{color:red;}
.tagValue{color:blue;}
.HTMLComment{font-style:italic;color:green;}
</style>
</head>
<body>
<?php
echo "<pre>".$document."</pre>";
?>
</body>
</html>
----
Server IP: 69.147.83.197
Probable Submitter: 79.177.105.104
----
Manual Page -- http://www.php.net/manual/en/function.highlight-string.php
Edit -- https://master.php.net/note/edit/81257
Del: integrated -- https://master.php.net/note/delete/81257/integrated
Del: useless -- https://master.php.net/note/delete/81257/useless
Del: bad code -- https://master.php.net/note/delete/81257/bad+code
Del: spam -- https://master.php.net/note/delete/81257/spam
Del: non-english -- https://master.php.net/note/delete/81257/non-english
Del: in docs -- https://master.php.net/note/delete/81257/in+docs
Del: other reasons-- https://master.php.net/note/delete/81257
Reject -- https://master.php.net/note/reject/81257
Search -- https://master.php.net/manage/user-notes.php