#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    5
    Rep Power
    0
    I am trying to display the source of HTML files withing pages on my site. What is the best way to display the code so that it doesen't get parsed as HTML?

    veggie2u
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Melbourne, Victoria, Australia
    Posts
    104
    Rep Power
    14
    The most common way is to use the <PRE> tag which literally takes pre-formatted text and displays it as is.

    Otherwise if that doesn't suit you then you can just use htmlspecialchars() and other related php functions to remove the dangerous characters that the browser might try to render as html.

    Hope that helps,

    Josh
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    There is a PHP function for showing the source of files. I can't remember the syntax, but it should work no problem with html files. it even adds color highlighting. create a php file that is passed the name of the file you want to show as source.

    show_source.php
    ****************
    <html>
    <body>
    <? show_source($file_name); ?>
    </body>
    </html>

    It's that easy, but I can't remember the function name...

    ---John Holmes...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Melbourne, Victoria, Australia
    Posts
    104
    Rep Power
    14
    I believe the functions you are talking about are highlight_string() and highlight_file().

    I think they only highlight syntax for php not html... it probably all comes out grey or something - but if you're displaying php then go for it, it's pretty cool.

    Josh
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    5
    Rep Power
    0
    Hello,

    The <pre> command won't work, it just deals with preserving of formating of spaces and linefeeds. It there are other <tags> in there, they will get parsed as html.

    The show_source() command shows some promise, but I can't get it working as of yet. But, anyway, it says that it is for .php files only.

    I have tried to use egri_replace() to change '<' and '>' to < and >, but there are still some weird things that happen with appostropies, dashes and '' characters. I can't remove them.

    veggie2u
  10. #6
  11. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    <html>
    <body>
    <?php show_source("index.html"); ?>
    </body>
    </html>

    Works just fine...yes, it's all one color, though.

    A better option would be to pass it the name of the file to show.

    Call it like this...

    <a href="show_source.php?file=index.html">View Source</a>

    show_source.php
    *************************
    <html>
    <body>
    <? show_source($file); ?>
    </body>
    </html>
    *************************


    Or...if you want to read the entire file into a string, you can then run htmlspecialchars($string) and then echo it out.

    $file = file("index.html");
    $string = implode("",$file);
    $fixed_string = htmlspecialchars($string);
    echo $fixed_string;

    ---John Holmes...

    ------------------
    *************************************************************
    * The manual can probably answer 90% of your questions...
    *
    * PHP Manual. www.php.net/manual
    * MySQL Manual: www.mysql.com/documentation/mysql/bychapter
    *************************************************************
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    5
    Rep Power
    0
    Thanks all, I will give some of your ideas a try John - Thanks.

    veggie2u
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    5
    Rep Power
    0
    OK, I am trying to write an HTML file editor, and I now have it reading in a file, and dealing with the tags properly like suggested, but the problem now is on saving. When I save the contents of the textarea, everything is fine except for a couple of characters, like "'" and "~". These are saved with a "/" saved inserted in front of it. What is the best way to get rid of these before saving?

    veggie2u
  16. #9
  17. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    Run stripslashes() on the data before you save it, or turn off magic_quotes in your php.ini file.

    ---John Holmes...
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    0
    Rep Power
    0
    One more way is to use special characters


    HTML code Special characters

    < &amp;li;
    > &amp;gt;
    " &amp;quot;
    & &amp;amp;


    [This message has been edited by pkringle (edited November 22, 2000).]

IMN logo majestic logo threadwatch logo seochat tools logo