Solved

Single Quote Escape (\') Insertion Problem while writing and reading back a text file.   How to handle?

Posted on 2006-07-03
6
296 Views
Last Modified: 2013-12-12
Hello!    Everyone, I have a problem of BACK SLASH  INSERTION.

I am developing a Blog or Comment submission interface through Web_PHP.   Since I am dealing not only English but also other languages, I use UTF-8 as default charcter set at Apache.     I used a simple functions to write and read the UTF-8 text files.  

   fwrite();    and    readfile($filename);

Here comes a problem.   All single quotes are modified to have back slash (\), and each time read and write that text, back slash are increasingly inserted.   See example below.    

   Qu\'est ce que s\'est passé.
   Alors les résultats de recherche n\'ont plus le lien d\'agrafes de multimédia.

How to stop inserting those extra slashes?   Suggestion please!!    Help!
0
Comment
Question by:mkido
6 Comments
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 20 total points
ID: 17034641
These back slashes are just escaped

You can use the stripslashes function (http://au.php.net/manual/en/function.stripslashes.php)
for example;
<?php
$str = "Is your name O\'reilly?";

// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>

to strip the slashes.
0
 
LVL 3

Accepted Solution

by:
NewJorg earned 40 total points
ID: 17036300
I assume that you read the file, show it in a form, edit the text in the browser and send it to the server which writes it back to the file?

then you should turn off the magic_quotes_gpc in php.ini. If you cant change it the suggestion from Raynard7 on the userinput will do fine.
0
 
LVL 20

Assisted Solution

by:Muhammad Wasif
Muhammad Wasif earned 20 total points
ID: 17041678
Its better to use stripslashes() in this way.

I am modifiying Raynard7's example
<?
$str = "Is your name O\'reilly?";

//first check if u need to remove the slashes or not
// by doing this you dont need to turn off the magic_quotes_gpc in php.ini
if(get_magic_quotes_gpc())
      echo stripslashes($str);
else
      echo $str;
?>

for more info http://www.php.net/get_magic_quotes_gpc
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:mkido
ID: 17043088
Hello everyone, thank you very much.  

I will soon try to see what will happen by turning off,

   magic_quotes_gpc in php.ini

then, get back here.     Then, I will try,

   stripslashes().

Thanks.
0
 

Author Comment

by:mkido
ID: 17044149
Dear wasifq,

tunring off
  magic_quotes_gpc    in php.ini
worked.     wonderful!
I saw that when fwrite() wrote down the french text, it aldreay inserted the slash (\) in front of quote (').   Thus, I think it is better to prevent the insertion while PHP writes text down.    

Regarding stripslashes(), let me try a little later.   Thanks.
0
 

Author Comment

by:mkido
ID: 17052561
Thank you,
  Raynard7,  NewJorg,  and wasifq:

stripslashes() works well too.   I prefer to turn off "magic_quotes_gpc" in "php.ini" file.    Thanks for your help!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question