Showing source

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Czar
Forum Commoner
Posts: 58
Joined: Sun Dec 29, 2002 11:17 am

Showing source

Post by Czar »

What is the best way to show code on php page, like code snippets here(ew). I load the source to DB and read it from there and i want to show the source code so, that code won't be parsed, not even html. Oh, and also; How to keep line breaks in right place so, that the code block wouldn't look like a table with a thousand characters in it?

Thanks.

BTW. The code doesn't have to be highlighted.
Phodetheus
Forum Newbie
Posts: 1
Joined: Wed Jul 02, 2003 3:23 am
Location: Kent, UK
Contact:

Post by Phodetheus »

Hi

First off make sure you omit the <? or <?php tag that tells the server to run your code, that will cause your code to be displayed.

If you surround the code in the <PRE></PRE> tag this will keep the code looking exactly as you have it in the source with line breaks and all.
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post by Drachlen »

Ripped from PHPBB:

Code: Select all

<?php
preg_replace('#(<)([\/]?.*?)(>)#is', '<\2>', $variable);

?>
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

another useful tag (for html) is <xmp> </xmp> which turns off the browsers html interpreter while active

so
<xmp><b>bold</b></xmp>
would show <b>bold</b> on the page.

to get php to nicely colour the code, just output the string through an
echo highlight_string($data); call.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

http://www.w3schools.com/xhtml/xhtml_reference.asp
http://www.w3schools.com/html/html_reference.asp

those links have the html/xhtml tags. i know there's multiple tags that will work. pic one that works with both, because html is being deprecated for xhtml

and you need to replace the < with < and > with >
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Also the PHP-functions highlight_file() and highlight_string() are useful. They basically display PHP-code exactly the same way as phpBB (e.g. this board).
Post Reply