PHP Post is returning the single quote character

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Zander1983
Forum Newbie
Posts: 20
Joined: Mon Mar 21, 2011 2:26 pm

PHP Post is returning the single quote character

Post by Zander1983 »

Hi
I have a simple form and when the user submits, php is putting a \ before every single quote entered in the field. So for example, if a user enters O'Neill, once i do
$lastName = $_POST["lastname"];

$lastName comes back as: O\'Neill

is there some way I can turn this off?
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: PHP Post is returning the single quote character

Post by fugix »

try this....

Code: Select all

$lastName = stripslashes($_POST["lastname"]);
Zander1983
Forum Newbie
Posts: 20
Joined: Mon Mar 21, 2011 2:26 pm

Re: PHP Post is returning the single quote character

Post by Zander1983 »

I found this solution. I put this script in a file, and included it into all my processing scripts. It strips the lashes automatically so no need to do striplashes() for each post.


<?php
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
function GPCStrip($arr)
{
if (is_array($arr))
{
foreach ($arr AS $arrKey => $arrVal)
{
$arr[$arrKey] = GPCStrip($arrVal);
}
}
else if (is_string($arr))
{
$arr = stripslashes($arr);
}
return $arr;
}
$_GET = GPCStrip($_GET);
$_POST = GPCStrip($_POST);
$_COOKIE = GPCStrip($_COOKIE);
if (is_array($_FILES))
{
foreach ($_FILES AS $key => $val)
{
$_FILES[$key]['tmp_name'] = str_replace('\\', '\\\\', $val['tmp_name']);
}
}
$_FILES = GPCStrip($_FILES);
}
if (function_exists('set_magic_quotes_runtime'))
{
set_magic_quotes_runtime(0);
}
?>
Post Reply