Taorluath Posted January 2, 2008 Share Posted January 2, 2008 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? ??? <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($fr, 1024); echo $line; fclose($fr); } ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/84199-reading-files-with-s-or/ Share on other sites More sharing options...
GingerRobot Posted January 2, 2008 Share Posted January 2, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84199-reading-files-with-s-or/#findComment-428785 Share on other sites More sharing options...
Taorluath Posted January 5, 2008 Author Share Posted January 5, 2008 Awesome! 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 htmlentities($text, ENT_QUOTES); and be displayed. (The display looks exactly right.) then it is written back to the file through html_entity_decode($text, ENT_QUOTES); but it is written having slashes before any quotes. How do I fix this? Quote Link to comment https://forums.phpfreaks.com/topic/84199-reading-files-with-s-or/#findComment-431336 Share on other sites More sharing options...
kratsg Posted January 5, 2008 Share Posted January 5, 2008 stripslashes($text); http://us.php.net/manual/en/function.stripslashes.php Quote Link to comment https://forums.phpfreaks.com/topic/84199-reading-files-with-s-or/#findComment-431339 Share on other sites More sharing options...
Taorluath Posted January 5, 2008 Author Share Posted January 5, 2008 Thank you so much! That totally fixed everything! This forum is pretty much the best php forum I've come across. Quote Link to comment https://forums.phpfreaks.com/topic/84199-reading-files-with-s-or/#findComment-431347 Share on other sites More sharing options...
kratsg Posted January 5, 2008 Share Posted January 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84199-reading-files-with-s-or/#findComment-431348 Share on other sites More sharing options...
PFMaBiSmAd Posted January 5, 2008 Share Posted January 5, 2008 The final escaping was probably done when the data was submitted from the form, due to the magic quotes setting. Quote Link to comment https://forums.phpfreaks.com/topic/84199-reading-files-with-s-or/#findComment-431349 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.