highlight_string not returning properly
Posted: Tue Dec 08, 2009 5:04 am
Ive created a class which formats strings passed to it and highlights any code to it.
Recently i fixed a problem where there would be multiple printout's if at "$1" was found in the string, however when i try to implement the highlight_string() function to highlight any code, it returns something odd and im not exactly sure why.
The code in question is around lines 82-109 where the formatting is taking place.
Any help would be appreciated.
The Class:
When this line is executed:
print highlight_string($codeArr[$i], true);
I get the following output:
Recently i fixed a problem where there would be multiple printout's if at "$1" was found in the string, however when i try to implement the highlight_string() function to highlight any code, it returns something odd and im not exactly sure why.
The code in question is around lines 82-109 where the formatting is taking place.
Any help would be appreciated.
The Class:
Code: Select all
class VNCode{
#
# Encode Function
#
# This is the only public function and
# is used to encode the desired string.
#
public function encode($string){
// If the string is not empty
if(!empty($string)){
// Create the regex array for tags
$search = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[quote\](.*?)\[\/quote\]/is',
'/\[quote\=(.*?)\](.*?)\[\/quote\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[url\](.*?)\[\/url\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
'/\n/s',
'/\t/s');
// Create the replacement HTML array tags
$replace = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
'<div class="quotecontent">$1</div>',
'<div class="quotetitle">$1 wrote:</div><div class="quotecontent">$2</div>',
'<img src="$1" />',
'<a href="$1">$1</a>',
'<a href="$1">$2</a>',
'<br>',
' ');
// Convert any html characters to encoded characters
$string = htmlspecialchars($string);
// Create the code language array
$languages = $this->getLanguages();
// Create the code boxes
foreach($languages as $lang){
// Split each tag
$codeArr = preg_split($lang['tag'], $string, -1, PREG_SPLIT_OFFSET_CAPTURE);
// Remove anything other than the tags
array_splice($codeArr, 0, 1);
foreach($codeArr as $code){
// Replace the existing text with the new code box
$string = preg_replace($lang['regex'], $this->formatCode($code[0],$lang['lang']), $string);
}
}
// Return the final encoded string
return preg_replace($search, $replace, $string);
}else{
// Return false if no string was sent
return false;
}
}
#
# FormatCode Function
#
# This function is responsible for replacing
# any specified code tags into code boxes.
# All tags are pre-specified.
#
private function formatCode($string,$type){
if(!empty($string)){
// Check each language available and set the appropreate language
foreach($this->getLanguages() as $language){
if(in_array($type, $language)){
$code = $language['lang'];
}
}
// Split the code into lines
$codeArr = preg_split('/\\n/s', $string);
// Create the code box header
$newCodeArr[] = "<div class='codebox'><div class='codeheader'>{$code}</div><div class='codeholder' style='max-height: 300px;'>";
for($i = 0,$j = 1; $i <= count($codeArr); $i++,$j++){
// Replace any $ symbols with their HTML equivilient ( DO NOT REMOVE THIS LINE )
$codeArr[$i] = preg_replace('/\$/','$',$codeArr[$i]);
if($i % 2){
// For each line of code, check for a close tag
if(preg_match('/\[\/(.*?)\]/i', $codeArr[$i], $matches, PREG_OFFSET_CAPTURE)){
$newCodeArr[] = "<div class='li2'>$j. ".preg_replace('/\[\/(.*?)\]/is', '', $codeArr[$i])."</div>";
break;
}
// If no close tag exists, store the new line of code.
$newCodeArr[] = "<div class='li2'>$j. {$codeArr[$i]}</div>";
}else{
// For each line of code, check for a close tag
if(preg_match('/\[\/(.*?)\]/i', $codeArr[$i], $matches, PREG_OFFSET_CAPTURE)){
$newCodeArr[] = "<div class='li1'>$j. ".preg_replace('/\[\/(.*?)\]/is', '', $codeArr[$i])."</div>";
break;
}
// If no close tag exists, store the new line of code.
$newCodeArr[] .= "<div class='li1'>$j. {$codeArr[$i]}</div>";
}
print html_entity_decode(highlight_string(htmlentities($codeArr[$i]), true));
}
// Close the code box
$newCodeArr[] = "</div></div>";
// Return the HTML code for the code box
return implode($newCodeArr);
}else{
// Return false if no code was submitted
return false;
}
}
#
# getLanguages Function
#
# This function defines languages and
# regular expressions used in order to
# determine the current tag being processed.
#
private function getLanguages(){
return array(
"php" => array( "lang" => "php", "tag" => '/\[php\]/sx', "regex" => '/\[php\](.*?)\[\/php\]/is'),
"html" => array( "lang" => "html", "tag" => '/\[html\]/sx', "regex" => '/\[html\](.*?)\[\/html\]/is'),
"sql" => array( "lang" => "sql", "tag" => '/\[sql\]/sx', "regex" => '/\[sql\](.*?)\[\/sql\]/is'),
"code" => array( "lang" => "code", "tag" => '/\[code\]/sx', "regex" => '/\[code\](.*?)\[\/code\]/is'),
"code=php" => array( "lang" => "php", "tag" => '/\[code\=php\]/sx', "regex" => '/\[code\=php\](.*?)\[\/code\]/is'),
"code=sql" => array( "lang" => "sql", "tag" => '/\[code\=sql\]/sx', "regex" => '/\[code\=sql\](.*?)\[\/code\]/is'),
"code=html" => array( "lang" => "html", "tag" => '/\[code\=html\]/sx', "regex" => '/\[code\=html\](.*?)\[\/code\]/is')
);
}
};print highlight_string($codeArr[$i], true);
I get the following output:
Code: Select all
function encode($string){ $search = array( '/\[code\](.*?)\[\/code\]/is', '/\[php\](.*?)\[\/php\]/is' ); $replace = array( "<div class='codebox'><div class='codeheader'>Code: </div><div class='codeholder'>$1</div></div>", $this->colourCode('$1') ); return preg_replace($search, $replace, $string); } function colourCode($string){ $string = highlight_string($string); return "<div class='codebox'><div class='codeheader'>Code: </div><div class='codeholder'>{$string}</div></div>";