Solved

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

Posted on 2006-07-03
6
298 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

821 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