Forum Moderators: coopster

Message Too Old, No Replies

Backslashes are being added before every single quote in PHP, help!

Using PHP to save form contents to text file, then display contents in HTML

         

Chad_Roe

8:33 am on Nov 4, 2006 (gmt 0)

10+ Year Member



I am writing a basic PHP news administration system, but am having a little problem. Wherever there is a single quote, a backslash is being added before it. My news system works this way; I have a set of HTML forms on one page that save their contents to a text file using PHP. I then use PHP to load and reverse the contents of that text file (newest news first) and display (echo) it on the index page. The only problem is, wherever there is a single quote, a backslash is being added right before it.

How do I stop this from happening? Below are pieces of the code I am using.

Save Code:


<?php
  $news_date = date("l n/d/y - g:i A");
  $news_file = ("news.txt");
  $fp = fopen($news_file, "a+");
   fputs ($fp, "form data goes here\n");
   fclose($fp);
   echo "<script>location.href='preview.php'</script>";
?>

Load, Reverse, Display Code:


<?php
  $data = file('news.txt');
  $data = array_reverse($data);
   foreach($data as $element) {
    $element = trim($element);
    $pieces = explode("¦", $element);
     echo $pieces[2] . $pieces[1] . "" . $pieces[0] . "";
   }
?>

Any help will be greatly appreciated.

Chad_Roe

10:01 am on Nov 4, 2006 (gmt 0)

10+ Year Member



Well, I have come up with a temporary solution, but I still need some help on the correct way to fix this problem.

I simply use the PHP string replace to remove all instances of \' in the data, just before it is displayed (echo) in the HTML. Like I said, this is only a temporary solution, and I still need some help actually saving the data to the text file without any instances of \' being in it.

PHP String Replace Code:

 <?php
   $data = file('news.txt');
   $data = str_replace("\'", "'", $data);
   $data = array_reverse($data);
     foreach($data as $element) {
       $element = trim($element);
       $pieces = explode("¦", $element);
       echo $pieces[2] . $pieces[1] . "" . $pieces[0] . "";
     }
 ?>

Again, any help will be greatly appreciated.

Chad_Roe

11:11 am on Nov 4, 2006 (gmt 0)

10+ Year Member



I also need the same help on double quotes ( " ).

leadegroot

11:20 am on Nov 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There must have been an addslashes call when the data was saved.
Look at the doc for that and stripslashes
stripslashes is the function best used to remove the slashes
Hope it helps!

eelixduppy

12:28 pm on Nov 4, 2006 (gmt 0)



Sounds likes you have magic_quotes [us3.php.net] enabled on your server. As was stated, use stripslashes [us3.php.net].