Author Topic: Reading Files with >'s or <'s  (Read 664 times)

0 Members and 1 Guest are viewing this topic.

Offline TaorluathTopic starter

  • Irregular
  • Posts: 30
    • View Profile
Reading Files with >'s or <'s
« on: January 02, 2008, 05:44:30 PM »
I was trying to make an HTML editor for my webpage in PHP.  First I had to make a script to display what is in the HTML documents, and in doing so, I encountered a problem.

Whenever I use fgets() to read a line out of any document with >'s or <'s, it just doesn't display anything.
And since HTML is obviously going to have a lot of <'s or >'s, this makes it hard to view and edit HTML files.
Do I need another function to read and write in files with these signs or is there some other way to use fgets()?

Here's a little bit of the code I was using. Maybe it's my fault it's not working? ???
Code: [Select]
<html><body>
<?php
$here 
"makey-makey, eggs and bakey";
$filename $_POST["filename"];


   
$fr fopen($filename'r');
       if(!
$fr) {
fclose($fr);
      
$fr fopen($filename,'w');
      if(!
$fr) {
         echo 
"Could not create the counter file!";
         exit;
      }
      
fputs($fr$here);
      
fclose($fr);
 } else {
      
$line fgets($fr1024);
 
echo $line;
     
fclose($fr);
      
   }
?>


</body></html>

Offline GingerRobot

  • Guru
  • Fanatic
  • *
  • Posts: 4,133
  • Gender: Male
  • Call me Ben
    • View Profile
Re: Reading Files with >'s or <'s
« Reply #1 on: January 02, 2008, 06:26:42 PM »
The problem is that if there are tags in the text, the browser believes they are HTML tags, and gets confused.

You need to use the htmlentities() function. It will change tags,quotes,symbols etc to their relevant HTML character code.


Offline TaorluathTopic starter

  • Irregular
  • Posts: 30
    • View Profile
Re: Reading Files with >'s or <'s
« Reply #2 on: January 05, 2008, 02:07:35 PM »
Awesome! ;D
I've got it displaying right, as in, it looks exactly like what's in the file. But then I have it use fputs() to write the revised version back to the file. But when it writes it back, all the quotes get slashes in front of them, like (\").

First I have the html file go through
Code: [Select]
htmlentities($text, ENT_QUOTES); and be displayed. (The display looks exactly right.)
then it is written back to the file through
Code: [Select]
html_entity_decode($text, ENT_QUOTES); but it is written having slashes before any quotes.
How do I fix this?

Offline kratsg

  • Devotee
  • Posts: 896
    • View Profile
Re: Reading Files with >'s or <'s
« Reply #3 on: January 05, 2008, 02:10:03 PM »

Offline TaorluathTopic starter

  • Irregular
  • Posts: 30
    • View Profile
Re: Reading Files with >'s or <'s
« Reply #4 on: January 05, 2008, 02:26:15 PM »
Thank you so much!
That totally fixed everything! This forum is pretty much the best php forum I've come across.

Offline kratsg

  • Devotee
  • Posts: 896
    • View Profile
Re: Reading Files with >'s or <'s
« Reply #5 on: January 05, 2008, 02:26:57 PM »
What I'm still wondering about is why it would escape quotes when you write back to an html file... I never had that problem o_O

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Reading Files with >'s or <'s
« Reply #6 on: January 05, 2008, 02:29:48 PM »
The final escaping was probably done when the data was submitted from the form, due to the magic quotes setting.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.