renfley Posted March 28, 2010 Share Posted March 28, 2010 Im trying to show code on my website that is properly displayed with highlighting ExaMPLE: http://www.php.net/manual/en/function.mysql-affected-rows.php That is exactly what i am looking for, i wana be able to post example code Any help Would be Freaking Awsome Thx Quote Link to comment https://forums.phpfreaks.com/topic/196812-code-highlighting-on-page/ Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 Here you go http://php.net/manual/en/function.highlight-string.php Can I have my freaking awesome cookie now? Quote Link to comment https://forums.phpfreaks.com/topic/196812-code-highlighting-on-page/#findComment-1033174 Share on other sites More sharing options...
renfley Posted March 28, 2010 Author Share Posted March 28, 2010 i figured you send me to that LOL maybe i should re ask LOL <?php $codeToHighlight = " <?php echo 'This is a simple Echo Statment'; ?>"; highlight_string($codeToHighlight); ?> This will work fine but when i replace ' with " i get a bunch of errors? i was more or less looking for an alternative? Quote Link to comment https://forums.phpfreaks.com/topic/196812-code-highlighting-on-page/#findComment-1033179 Share on other sites More sharing options...
ignace Posted March 28, 2010 Share Posted March 28, 2010 That's because you should use \" instead of " Quote Link to comment https://forums.phpfreaks.com/topic/196812-code-highlighting-on-page/#findComment-1033182 Share on other sites More sharing options...
premiso Posted March 28, 2010 Share Posted March 28, 2010 Not sure if this helps, but you can use HEREDOC syntax: <?php $string = <<<CODE <?php echo "Test'"; ?> CODE; highlight_string($string); ?> Which should alleviate worrying about quotes etc. Quote Link to comment https://forums.phpfreaks.com/topic/196812-code-highlighting-on-page/#findComment-1033184 Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 Escape the double quote by using a backslash (\) before it, PHP will then skip over it without thinking it is the end of a string. Quote Link to comment https://forums.phpfreaks.com/topic/196812-code-highlighting-on-page/#findComment-1033186 Share on other sites More sharing options...
renfley Posted March 28, 2010 Author Share Posted March 28, 2010 premiso your solution works up to the point where one would wanna create a variable then the code wont show try this <?php $string = <<<CODE <?php $con= (andre) echo "Test"; ?> CODE; highlight_string($string); ?> - Quote Link to comment https://forums.phpfreaks.com/topic/196812-code-highlighting-on-page/#findComment-1033220 Share on other sites More sharing options...
renfley Posted March 28, 2010 Author Share Posted March 28, 2010 Solution !!!!!!!!!! in order to get everything included in the highlight field without worrying about hacks you can simply do the following Save the following as a regular text file in my case dbconnect.txt <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?> Then insert this little code <?php highlight_file("code/dbconnect.txt"); ?> And voila no need to worry about modifying your code for it to look the right way! Quote Link to comment https://forums.phpfreaks.com/topic/196812-code-highlighting-on-page/#findComment-1033239 Share on other sites More sharing options...
premiso Posted March 29, 2010 Share Posted March 29, 2010 Putting it into the file seems to be the wise choice. If, however, you plan on just displaying that file highlighted, you should be able to rename it to .phps (you may have to modify the apache httpd.conf and add the type) and it should show the source highlighted just fine. Quote Link to comment https://forums.phpfreaks.com/topic/196812-code-highlighting-on-page/#findComment-1033287 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.