Reputation: 5
I need to skip single quotes in some input data and I wanted to do it within a function (the function should include also other instructions, but I'm not writing them here for the sake of clarity). So I wrote the following function and tested its output inside and outside the function:
function quote_skip($data)
{
$data = str_replace("'", "\'", $data);
echo "Output inside the function quote_skip: ".$data." <br>";
return $data;
}
$test = "l'uomo";
quote_skip($test);
echo "Output outside the function quote_skip: ".$test."<br>";
The result is the follwing:
Output inside the function quote_strip: l\'uomo
Output outside the function quote_strip: l'uomo
So what happens is that when I echo the variable outside the function the backslash is not there anymore. Why does this happen? Is there a way to keep the backslash also outside the function?
I only know the basics of php and maybe the answer is very obvious, but I haven't been able to find anything in all the forums I have searched. If anyone has a solution it will be greatly appreciated.
Thank you.
Upvotes: 0
Views: 124
Reputation: 3933
It will work if you make it so the variable passes by reference:
function quote_skip(&$data) // use the `&`
{
$data = str_replace("'", "\'", $data);
echo "Output inside the function quote_skip: ".$data." <br>";
}
Demo: http://phpfiddle.org/lite/code/emr-5ap
Upvotes: 0
Reputation: 43552
Function is not the problem, your code bellow function is, where you don't echo function output:
$test = "l'uomo";
echo "Output outside the function quote_skip: ".quote_skip($test)."<br>";
Upvotes: 2