How to display PHP code in HTML

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeffey
    New Member
    • Aug 2007
    • 1

    How to display PHP code in HTML

    I have built my own searchable Code Snippet database and am having a problem displaying the search result code in the HTML page. Any suggestions on how I can do this?

    I also sometimes get a string error when adding code into the database.

    Thanks.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Try running the code through the htmlentities() function before displaying it in your HTML page. Also, wrap the code inside <pre> tags. They display stuff like linebreaks '\n' and tabs '\t' that HTML usually ignores.

    You can use the addslashes() function to remove most special characters that may cause string errors when you add stuff to your database. Just remember to use stripslashes() after you fetch it back from the database.

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Originally posted by jeffey
      I have built my own searchable Code Snippet database and am having a problem displaying the search result code in the HTML page. Any suggestions on how I can do this?

      I also sometimes get a string error when adding code into the database.

      Thanks.
      Welcome to TheScripts.com jeffey

      I've deleted the thread, that you posted accidently on Php-Articles section as per your Request.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Atli.

        Originally posted by Atli
        Just remember to use stripslashes() after you fetch it back from the database.
        Not sure if this is really necessary, unless you addslashes() twice. When MySQL saves an escaped string, it automatically strips out the slashes. For example:
        [code=mysql]
        SELECT 'A \'simple\' escaped quotes string';
        [/code]
        outputs: A 'simple' escaped quotes string

        Comment

        Working...