arneman Posted July 1, 2009 Share Posted July 1, 2009 I want to make a tutorial website, but making the PHP tutorial I ran into a little problem. I usually save my tutorials in MySQL in HTML. This works fine, the HTML is shown as text with the correct layout. The PHP only works partially. I want to do the following: <p>test</p> <?php highlight_string('echo "test";'); ?> On my screen i see, 'echo "test"' but without the highlights. Any suggestions on how to change this? Quote Link to comment Share on other sites More sharing options...
Brian W Posted July 1, 2009 Share Posted July 1, 2009 if you go to view source what do you get? Or if you have FF w/ Firebug, when you inspect the element what do you get? Quote Link to comment Share on other sites More sharing options...
arneman Posted July 1, 2009 Author Share Posted July 1, 2009 If I view source in Firefox I see the following: <?php highlight_string('Hello world! <?php phpinfo();?>') ?> Quote Link to comment Share on other sites More sharing options...
Brian W Posted July 1, 2009 Share Posted July 1, 2009 well, if your trying to output the code (w/o highlight), you need to use htmlentities() on the result. If you want the php being returned in the result to process as php, your need to use eval(). WARNING: eval() will allow several attacks on your site if you allow user submitted data to pass through it. For example, some one inputs <?php print_r($_SESSION); ?> or other sensitive data is echoed out to the end user. Quote Link to comment Share on other sites More sharing options...
arneman Posted July 1, 2009 Author Share Posted July 1, 2009 The MySQL data will be placed in there by me, so it should be harmless right? Quote Link to comment Share on other sites More sharing options...
Brian W Posted July 1, 2009 Share Posted July 1, 2009 unless your emo, harmless. lol I'm not sure of the performance related to using eval or other possible issues, I just know generally it's not a favored technique. Best, Brian Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 1, 2009 Share Posted July 1, 2009 You are trying to display php source code as part of a tutorial, using the highlight_string() function to give it some color, correct? So, are you doing this in a .php file, on a web server with php installed and functional? Quote Link to comment Share on other sites More sharing options...
arneman Posted July 1, 2009 Author Share Posted July 1, 2009 You are correct! In my PHP file, I query a MySQL database containing the complete tutorial written in HTML. As I said before, the HTML executes, but the PHP doesn't, or at least not correctly. Edit: Yes I am on a webserver with everything installed. Quote Link to comment Share on other sites More sharing options...
Brian W Posted July 2, 2009 Share Posted July 2, 2009 mis-communication the entire way... lol arneman, does it do what you want now? if so, PFMaBiSmAd, no worries. Quote Link to comment Share on other sites More sharing options...
arneman Posted July 2, 2009 Author Share Posted July 2, 2009 Well I tried the Eval() but the PHP manual on that part isn't completely clear to me. So I put the following in my DB: <?php $code = 'highlight_string(\'echo "test";\')'; eval($code); echo $code; ?> Unfortunately it doesn't work Quote Link to comment Share on other sites More sharing options...
arneman Posted July 2, 2009 Author Share Posted July 2, 2009 I think the error is in the way I put it on my screen: echo '<p>'.$tutorial.'</p>'; I think something is going wrong with the quotes? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 2, 2009 Share Posted July 2, 2009 If you put this php code - <?php highlight_string('echo "test";'); ?> into a .php file on a server with php installed and browse to the file, you get this resulting HTML content - <code><span style="color: #000000"> echo "test";</span> </code> eval() has absolutely nothing to do with this, don't attempt to use it. Quote Link to comment Share on other sites More sharing options...
arneman Posted July 2, 2009 Author Share Posted July 2, 2009 Nevermind I worked around it using a second field containing the PHP code. Thanks anyway. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 2, 2009 Share Posted July 2, 2009 I'm going to guess that you actually wanted to do the following - $some_code = '<p>test</p> <?php echo "test"; ?>'; // where $some_code is what you retrieved from the database? highlight_string($some_code); Which produces this HTML output - <code><span style="color: #000000"> <p>test</p> <br /><span style="color: #0000BB"><?php <br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"test"</span><span style="color: #007700">; <br /></span><span style="color: #0000BB">?></span> </span> </code> Quote Link to comment Share on other sites More sharing options...
arneman Posted July 2, 2009 Author Share Posted July 2, 2009 True, but doing that you will still see the <p> tags. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 2, 2009 Share Posted July 2, 2009 Based on that, I finally get what you are trying to do - <p>test</p> is HTML content on the page to serve as a title for the code that follows it. You should be using some type of a template with variables $title, $code or place holders {TITLE}, {CODE} that you replace with the actual values when you display the section of the page. And yes, the title and the code should be stored in separate columns in the table row. For the example you have been using - $title = 'test'; $code = highlight_string('echo "test";',TRUE); // either directly echo the template or have it exist in a variable that is echoed later echo "<p>$title</p> $code"; Quote Link to comment Share on other sites More sharing options...
arneman Posted July 2, 2009 Author Share Posted July 2, 2009 Yes I solved it liked that. Thanks for the quick and good help though Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.