highlight_string()

Started on the old forum in August 2012 · view the original in the archive · old links redirect here

3 replies · 598 views · started 14 years ago · 1 min read

3 participants
Ghost Senior · 366 posts #1

ok i know i started my own syntax highlighting project before. but i wanted to try and use the built in php syntax highlighter for experimental purposes.

any ideas why isnt this working.

 /** Show Post */
function Show_Post($Posts_To_Show, $File_Path)
{
$Show_Posts = file($File_Path);
for($k = 0; $k<$Posts_To_Show; $k++)
{
echo preg_replace('/<div class="codebox">(.*?)<\/div>/i', '<div class="codebox">'.highlight_string('\\1', true).'</div>', $Show_Posts[$k]);
}
} /** End Of Show Post Function */
//
//

//

its an attempt to use php built in highlighter.

but the resulting source code come back like this when displayed..

<div class="codebox"><code><span style="color: #000000">
 /** Show Post */
function Show_Post($Posts_To_Show, $File_Path)
{
$Show_Posts = file($File_Path);
for($k = 0; $k<$Posts_To_Show; $k++)
{
echo preg_replace('/<div class="codebox">(.*?)<\/div>/i', '<div class="codebox">'.highlight_string('\\1', true).'</div>', $Show_Posts[$k]);
}
} /** End Of Show Post Function */
</span></code></div>
//

//
//
//

basically just turning all text black. so its finding the string of code. adding the code tag and only 1 span style color.

i was wondering if it would possibly have something to do with the htmlentities i set on the string before adding it to database. then use this function to pull the string from the database then try to highlight it.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
arnage Lead · 811 posts #2

Yes, highlight_string() is doing html chars conversion it self.

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
Ghost Senior · 366 posts #3

i was hoping that wouldnt be the case.

as i need htmlentities() to be used so i dont have raw php code going into the database. or else if would make the text outside the codeboxes to become vunerable to attacks.

ive tried many things. including trying to html_entity_decode() just the text inside the codebox but it still returns black text rather than colored.

echo preg_replace('/<div class="codebox">(.*?)<\/div>/is', '<div class="codebox">'.highlight_string('<?php echo "$test"; ?>', true).'</div>', $Show_Posts[$k]);
//
//
//

works perfectly. so i know its an issue with the \1 or $1 regexing (ie: the grabbed code inside the box)

so i tried it with wrapping it in php tags too like so.

echo preg_replace('/<div class="codebox">(.*?)<\/div>/is', '<div class="codebox">'.highlight_string('<?php '.'\\1'.' ?>', true).'</div>', $Show_Posts[$k]);
//
//
//

but then that just sent back <?php \1 ?> rather than the text. although this time it was colored.

its just frustrating. this is suppose to be the easy route. rather than making your own. but ive had more issues trying to correct this than i had making my own upto the point where i had that commenting issue lol.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
something else Fellow · 2,296 posts #4

it sounds like you have another code that you have not showed us interfering with the code.... im guessing you could do a string replacement for the code and replace // with ///

Sign in to reply.

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close