Question

I'm trying to encode my script using base64 in php, but why my code automatic adds backslashes \ at single quotes ' or double quotes "
this is the code I am using to encode

$encode = $_POST ['encode'];
$encod = base64_encode ($encode);
echo "<form action='?' method='post'><textarea name='encode'></textarea><input type='submit' value='Encode'></form><textarea> ".$encod." </textarea>";

I use code at above, then I try to encode this script:

echo "just test";
echo 'or just test'; 

and result

PD9waHAgZWNobyBcImp1c3QgdGVzdFwiOw0KZWNobyBcJ29yIGp1c3QgdGVzdFwnOyA/Pg==

when I decode, result

echo \"just test\";
echo \'or just test\';

how to delete backslashes??

I've tried using str_replace on $encod, and stripslashes($encod) but it does not work.

No correct solution

OTHER TIPS

I have tried decoding your base64 encoded string and it yielded the same result. I tried encoding it again, slashes and all, and it yielded the same encoded string.

The problem is not that you are somehow getting unknown slashes out of the decoding process, but that the slashes exist there before you ever encoded the string.

The same string without slashes is this: PD9waHAgZWNobyAianVzdCB0ZXN0IjsNCmVjaG8gJ29yIGp1c3QgdGVzdCc7ID8+

Try decoding it.

I believe that the real solution to your problem can be found here:

Why are $_POST variables getting escaped in PHP?

I believe the string you are encoding in base64 was already encoded in json (because the POST has to be done with a json string, I assume).

Json's way to escape the quotes is what you describe to be your problem:

See: json_encode PHP

So the solution here would be to first decode from base64 (base64_decode [PHP]) then decode from json (jsoN_decode() [PHP]) or (JSON.parse() [JS])

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top