I've been pulling my hair out all day with some broken code. I posted a question early, but I've narrowed down the problem to the specific issue.
Previous question: Can't assess an array element after building array - a var_dump confirms element is there
Now the issue is with the base64 decode function. It is meant to return a string, but whenever you try to read the string it doesn't work.
See this simple test code...
error_reporting(E_ALL);
$encoded = base64_encode('my encrypted text');
$decoded = base64_decode($encoded, true);
echo "<br />";
printf('my encrypted text -> encoded to base64 = %s', $encoded);
echo "<br />";
printf('%s from base64 = %s', $endcoded, $decoded);
echo "<br />";
printf('calling $decoded to read string: result = %s', $decoded);
Here is the result
my encrypted text -> encoded to base64 = bXkgZW5jcnlwdGVkIHRleHQ=
Notice: Undefined variable: endcoded in /home/website/base64test.php on line 10
from base64 = my encrypted text
calling $decoded to read string: result = my encrypted text
Line 10 is this:
printf('%s from base64 = %s', $endcoded, $decoded);
Is this a bug? Or am I missing something.
How are you meant to read the result from the base64_decode() function after it has been stored in a variable.
You have a typo: endcoded:
printf('%s from base64 = %s', $endcoded, $decoded);
//-------------------------------^
Should be
printf('%s from base64 = %s', $encoded, $decoded);
//-------------------------------^
Check typo of $endcoded,
printf('%s from base64 = %s', $encoded, $decoded);
Related
I have a query string with URL-encoded symbols:
$wm_string = "LMI_MODE=1&LMI_PAYMENT_DESC=%CF%EE%E6%E5%F0%F2%E2%EE%E2%E0%ED%E8%E5+Plan+Z";
I need to convert it into JSON with PHP, but json_encode returns an empty string.
Here is my code in PHP:
parse_str($wm_string, $_REQUEST);
var_dump($_REQUEST);
echo "JSON:".json_encode($_REQUEST);
Here is the result:
array(1) { ["LMI_MODE"]=> string(46) "1?LMI_PAYMENT_DESC=Пожертвование Plan Z Online" } JSON:
What should I do?
UPDATE:
The expected result is:
{
"LMI_MODE":1,
"LMI_PAYMENT_DESC":"Пожертвование Plan Z Online"
}
UPDATE2:
The encoding is windows-1251, while json_encode seems to be expecting UTF-8. Is there a way to tell json_encode which encoding it should use while parsing?
Since json_encode does work with UTF-8 only, and the text is in windows-1251, it should be converted from that encoding to UTF-8.
$wm_string = "LMI_MODE=1&LMI_PAYMENT_DESC=%CF%EE%E6%E5%F0%F2%E2%EE%E2%E0%ED%E8%E5+Plan+Z+Online";
$wm_string = iconv("windows-1251", "UTF-8", $wm_string);
parse_str(urldecode($wm_string), $result);
echo "JSON:".json_encode($result, JSON_UNESCAPED_UNICODE);
Output:
JSON:{"LMI_MODE":"1","LMI_PAYMENT_DESC":"Пожертвование Plan Z Online"}
Try this:
$wm_string = "LMI_MODE=1LMI_PAYMENT_DESC=%CF%EE%E6%E5%F0%F2%E2%EE%E2%E0%ED%E8%E5+Plan+Z";
$wm_string = (parse_url(urldecode($wm_string)));
$wm_string = json_encode(urlencode($wm_string['path']));
echo "JSON: " . $wm_string;
Result:
JSON: "LMI_MODE%3D1LMI_PAYMENT_DESC%3D%CF%EE%E6%E5%F0%F2%E2%EE%E2%E0%ED%E8%E5+Plan+Z"
I know this is similar to the other two answers, but just wanted to point out that you don't need to and in fact shouldn't be using urldecode with parse_url. In a simpler approach you could parse the url string and convert the windows-1251 variable to utf8 like this.
<?php
$wm_string = "LMI_MODE=1&LMI_PAYMENT_DESC=%CF%EE%E6%E5%F0%F2%E2%EE%E2%E0%ED%E8%E5+Plan+Z+Online";
//Parse the url string
parse_str($wm_string, $args);
//Convert payment description to utf8
$args['LMI_PAYMENT_DESC'] = iconv("windows-1251", "UTF-8", $args['LMI_PAYMENT_DESC']);
echo "JSON: " . json_encode($args, JSON_UNESCAPED_UNICODE);
//JSON: {"LMI_MODE":"1","LMI_PAYMENT_DESC":"Пожертвование Plan Z Online"}
I need to base 64 encode parts of a URL for an S3 URL.
I'm left with something like:
http://d111111abcdef8.cloudfront.net/image.jpg?color=red&size=medium
&Expires=1357034400
&Signature=nitfHRCrtziwO2HwPfWw~yYDhUF5EwRunQA-j19DzZrvDh6hQ73lDx~-ar3UocvvRQVw6EkC~GdpGQyyOSKQim-TxAnW7d8F5Kkai9HVx0FIu- 5jcQb0UEmatEXAMPLE3ReXySpLSMj0yCd3ZAB4UcBCAqEijkytL6f3fVYNGQI6
&Key-Pair-Id=APKA9ONS7QCOWEXAMPL
As you can see Signature and Key Pair ID are encoded.
I need to use the above URL as a param in another URL.
I have base64 encoded (as to mask the domain, makes it a liitle prettier) and then URL encoded this.
My question is, with having certain params base 64 encoded, then base 64 encoding the entire string again, upon decode, will the original params such as Signature and Key Pair ID be readable?
Simple question, simple answer: Yes.
If you are going to do this, you will need to use "&" instead of "&" in the string you are encoding. Also, base64_decoding the entire encoded string, will only decode the last encoding.
An example:
$string1 = "This is a string";
$string2 = "This is another String";
$string1 = base64_encode( $string1 );
$string2 = base64_encode( $string2 );
echo $string1 . "<br />";
echo $string2 . "<br />";
$entity = "HTTP://www.google.com/?param1=" . $string1 . "¶m2=" . $string2;
$encoded_entity = base64_encode( $entity );
echo $encoded_entity . "<br />";
$decoded_entity = base64_decode( $encoded_entity );
echo $decoded_entity . "<br />";
This will output:
VGhpcyBpcyBhIHN0cmluZw==
VGhpcyBpcyBhbm90aGVyIFN0cmluZw==
SFRUUDovL3d3dy5nb29nbGUuY29tLz9wYXJhbTE9VkdocGN5QnBjeUJoSUhOMGNtbHVadz09JmFtcDtwYXJhbTI9VkdocGN5QnBjeUJoYm05MGFHVnlJRk4wY21sdVp3PT0=
HTTP://www.google.com/?param1=VGhpcyBpcyBhIHN0cmluZw==¶m2=VGhpcyBpcyBhbm90aGVyIFN0cmluZw==
So as you can see, you can decode the entire string, but only the string you encoded will be decoded. Not all the levels of the encoded string. For that you will first have to decode the string, and then after that, decode the parameters.
Is there any way to decode this string??
Actual string : 其他語言測試 - testing
base64 encode while sending on mail as subject as
"=?iso-2022-jp?B?GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==?="
<?php
echo base64_decode('GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==');
?>
This is base 64 encode, I couldn't decode it to actual Chinese string.Since it has been encoded using "iso-2022-jp", I have also tried online base64decode.org site to decode this string, but I couldn't find the original string, how can I do that?
Use iconv():
<?php
$input = base64_decode('GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==');//$BB6B>8l8#B,;n(B - testing
$input_encoding = 'iso-2022-jp';
echo iconv($input_encoding, 'UTF-8', $input); //其他語言測試 - testing
?>
What you are looking at is MIME header encoding. It can be decoded by mb_decode_mimeheader(), and generated by mb_encode_mimeheader(). For example:
<?php
mb_internal_encoding("utf-8");
$subj = "=?iso-2022-jp?B?GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==?=";
print mb_decode_mimeheader($subj);
?>
其他語言測試 - testing
(The call to mb_internal_encoding() is necessary here because the contents of the subject line can't be represented in the default internal encoding of ISO8859-1.)
Try encoding the string to UTF-8 first and then encode it to base 64.
Same when decoding, decode the string from base64 and then from UTF-8.
This is working for me:
php > $base = "其他語言測試 - testing";
php > $encoded = base64_encode(utf8_encode($base));
php > $decoded = utf8_decode(base64_decode($encoded));
php > echo ($decoded === $base) . "\n";
1
I want to encode an email address into its corresponding character codes, so when it is printed the char codes are interpreted by the browser, but the robots get the encoded string instead of the interpreted one.
For example (1):
abc#abc.com
should be sent to the browser as (2) (whitespaces added so the browser shows it):
a ;b ;c ;@ ;a ;b ;c ;. ;c ;o ;m ;
so the human reads (1) and web robots read(2)
There should be an easy function or way to do this, but cannot find one.
YOu could try this:
<?php
$s = "abc#abc.com";
$obj = array_map(function($x){return "&#". strval(ord($x)) . ";";},str_split($s));
echo implode($obj);
?>
function encode_everything($string){
$encoded = "";
for ($n=0;$n<strlen($string);$n++){
$check = htmlentities($string[$n],ENT_QUOTES);
$string[$n] == $check ? $encoded .= "&#".ord($string[$n]).";" : $encoded .= $check;
}
return $encoded;
}
Found at:
http://php.net/manual/en/function.htmlentities.php
Here's a neat function I wrote to do something similar - not sure if it's exactly what you're looking for, but it uses php's ord function to take each character in a string and output its ascii equivalent:
$testString = "I hate – Character – 150" ;
function printAscii($string){
for ($n=0;$n<strlen($string);$n++){
echo "<pre>";
echo ord($string[$n]);
echo " ---->";
echo $string[$n];
echo "</pre>";
}
}
$test = printAscii($testString);
I've got a PHP script that is reading in some JSON data provided by a client. The JSON data provided had a single 'smart quote' in it.
Example:
{
"title" : "Lorem Ipsum’s Dolar"
}
In my script I'm using a small function to get the json data:
public function getJson($url) {
$filePath = $url;
$fh = fopen($filePath, 'r') or die();
$temp = fread($fh, filesize($filePath));
$temp = utf8_encode($temp);
echo $temp . "<br />";
$json = json_decode($temp);
fclose($fh);
return $json;
}
If I utf8 encode the data, when I echo it out I see nothing where the quote should be. If I don't utf8 encode the data, when I echo it out I see the funny question mark symbol �
Any thoughts on how to actually see the proper character??
Thanks!
Is it possibe that the server is sending the json data in an encoding like windows-1252? That codepage has some smart code characters where iso-8859 has control characters. Could you try to use iconv("windows-1252", "utf-8", $temp) instead of utf8_encode. Even better would be if the server already sends utf-8 encoded json, since that is the recommended encoding per rfc4627.
The issue is more on the side, that generates the JSON file. There you should escape the ' by \'
If you can't modify this part, you should do it like this with addslashes:
$temp = fread($fh, filesize($filePath));
$temp = utf8_encode($temp);
echo $temp . "<br />";
$temp = addslashes($temp);
$json = json_decode($temp);
Can you possibly do a string replace assuming the data is all utf8?
$text = str_replace($find, $replace, $text);
Looking for the characters below?
'“' // left side double smart quote
'â€' // right side double smart quote
'‘' // left side single smart quote
'’' // right side single smart quote
'…' // elipsis
'—' // em dash
'–' // en dash