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?
Advertisement
Answer
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!