Re: Syntax Highlighting variables not appearing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Tom Whitbread wrote:
> Can anyone explain why this is happening.
>
> I am using the following code For giving code examples on my website
> with syntax highlighting
>
> $text = '<?php \$my_code = "echo 'text text text text'; \$foo =
> 'bar';"; example_function(\$my_code); ?&gt';

This is not a valid PHP statement.

The ' directly before 'text text text text' will match the first ', and
the rest of this is gibberish.

Let's assume, however, that you actually have \ in front of each embedded
' in your string.

To avoid confusion, I'd also change each \$ to \\$ so that it is clear
that you want the \ to be embedded in your string.

> $body = preg_replace('/&lt;(.*?)&gt;/es', 'highlight_string("<\\1>",
> true)', $text);

At this point, you are replacing HTML entities &lt; and &gt; with their
corresponding charactes < and > which is fine, I suppose, but...

> echo $body; //Returns: <?php = 'echo \'text text text text\'\';  =
> \'bar\'; highlight_string(, true); ?>

Here you are *NOT* going to see the PHP source, because to the *BROWSER*
the < and > look like an HTML tag.

The browsers will simply IGNORE tags they don't understand and won't
display/do anything with them.

> 		//I want:  <?php $my_code = "echo 'text text text text'; $foo = 'bar';";
> example_function($my_code); ?>

You may or may not need to NOT do the ereg_replace you are doing, and you
may or may not want to use http://php.net/htmlentities and/or
http://php.net/highlight_string

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux