Re: Syntax Highlighting variables not appearing

From: Date: Mon, 28 Feb 2005 22:47:47 +0000
Subject: Re: Syntax Highlighting variables not appearing
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-209632@lists.php.net to get a copy of this message
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 = '&lt;?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

« previous php.general (#209632) next »