Reputation: 41
I want to encode and decode in php using base64, but the encode and decode function does not give me correct output. I am using code from online php functions.
I encode this string
"best arabic songs loves 2013 nonstop أفضل من الأغاني الحب الجديد كلمات العربي"
and get the output:
"YmVzdCBhcmFiaWMgc29uZ3MgbG92ZXMgMjAxMyBub25zdG9wINij2YHYttmEINmF2YYg2KfZhNij2LrYp9mG2Yog2KfZhNit2Kgg2KfZhNis2K/ZitivINmD2YTZhdin2Kog2KfZhNi52LHYqNmK"
When I use this php code it does not give a correct encode and decode string:
$str = 'best arabic songs loves 2013 nonstop أفضل من الأغاني الحب الجديد كلمات العربي';
$decodestr = base64_decode($str);
echo 'decode = '.$decodestr
$encodestr = base64_encode($decodestr );
echo '<br>';
echo 'encode = '.$encodestr;
i got this output :
decode = më-j¶›‰Ë(ž%¢÷¬ÛMwž‰ì¶Š
encode = bestarabicsongsloves2013nonstoo=
Can any one help me with how I can get correct encode and decode string using base64 encode and decode function in php?
Upvotes: 0
Views: 6012
Reputation: 21
You can also use this PHP to encode/decode your base64.
<?php
/*To encode A string into base64 in PHP: */
$str = "I am Krypton-X"; /*This is your string*/
$encode = base64_encode($str); /*Encode*/
echo("$encode"); /*print base64*/
?>
To decode base64 into string in PHP
<?php
$base = "SSBhbSBLcnlwdG9uLVg=";
/*Your base64 encoded data */
$decode = base64_decode($base); /*Decode*/
echo("$decode"); /* print string */
?>
Upvotes: 0
Reputation: 502
the function base64_decode only decodes already base64encoded chunk . In your code you are trying to base64decode a plain text which is causing issues.
<?php $str = 'best arabic songs loves 2013 nonstop أفضل من الأغاني الحب الجديد كلمات العربي'; $encodestr = base64_encode($str); echo 'Base64 encoding of "best arabic songs loves 2013 nonstop أفضل من الأغاني الحب الجديد كلمات العربي" = '.'<br><br>'.$encodestr; $decodestr=base64_decode($encodestr); echo '<br><br><br>'; echo 'Base64 decoding of "best arabic songs loves 2013 nonstop أفضل من الأغاني الحب الجديد كلمات العربي" = '.'<br><br>'.$decodestr; ?>
Try this code: it works fine for me!
Upvotes: 1