Solved

php strip apostrophes

Posted on 2006-11-15
8
1,271 Views
Last Modified: 2009-12-16
Is there a way to put something above a file to strip '  from all forms and then be able to use the HTTP_POST_VARS later on?
0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17949912
Yes, but I strongly suspect there's a more direct way of solving the problem you're facing.  What IS the original problem you're facing?

Meanwhile, this will do what you've asked:

function inexplicably_remove_single_quotes(/* string */ $s) {
    return str_replace("'", '', $s);
}

$_POST = array_map($_POST, $s);

(Though I've here used the more up-to-date $_POST you could do the same thing with $HTTP_POST_VARS if necessary.)
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17949933
Heh, no that won't; that won't do anything at all.  THIS would do it:

$_POST = array_map($_POST, 'inexplicably-remove_single_quotes');
0
 

Expert Comment

by:choosebooks
ID: 17951706
Are you familiar with addslashes()?  This will escape all your quotes.  
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17951720
Well, the way to handle quotes depends on where they're going.  If into a MySQL database then mysql_real_escape_string() would be more appropriate.  If into an HTML page then htmlentities() would be more appropriate.  addslashes() is a generic way of escaping, but often isn't the right way.
0
 

Author Comment

by:jackjohnson44
ID: 17966165
I am posting the page back to itself, and the form repopulates.
If it posts back and it has a quote, it will keep putting slashes in the text box.
I totally want to turn them off and if I need them I can add them myself.
Is there a way to turn it off?
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17966207
Yes, it's an option in php.ini -- see http://www.php.net/manual/en/security.magicquotes.disabling.php

If you're on a server where you can't control php.ini, the code I posted is a workaround: it just takes the slashes back out after PHP adds them.  It's annoying, yes, but once you've done it the effect is as though PHP never poked its head into it in the first place.
0
 

Author Comment

by:jackjohnson44
ID: 17966262
Is this what you are talking about?
$_POST = array_map($_POST, 'inexplicably-remove_single_quotes');

Can I just put it in an include file?

What does this mean: inexplicably-remove_single_quotes

Does this do anything to: HTTP_POST_VARS?

This is what I want to change.

0
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 500 total points
ID: 17966397
Wait, wait, that's not quite right: you don't want to remove QUOTES, you want (and this sounds like a much more reasonable plan to me) to just remove ESCAPING of quotes.

For that just use stripslashes().

$HTTP_POST_VARS = array_map($HTTP_POST_VARS, 'stripslashes');


The inexplicably_remove_single_quotes (see my first comment for the definition of that function) actually removed the QUOTES, which is what you first asked.  To just undo the escaping all you need is the built-in stripslashes function.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

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…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 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