I am trying to deduce a file from the form, I have a problem when displaying Cyrillic
Php (7.1)
<?php
header("Content-type: application/json; charset=utf-8");
file_put_contents('export.json', json_encode($_REQUEST) );
?>
Output
You_name field is written in characters.
{"Entry_ID":"71","You_name":"\u041a\u043e\u0437\u043b\u0430\u043d\u0431\u0435\u043a \u0410\u043c\u0438\u0440 \u041f\u043e\u043c\u0438\u0434\u043e\u0440\u043e\u0432\u0438\u0447","You_phone":"7 (902) 998 1019","You_date":"19\/06\/2019"}
6 year old answer still stands: Why does the PHP json_encode function convert UTF-8 strings to hexadecimal entities?
json_encode( $text, JSON_UNESCAPED_UNICODE );
Example:
/tmp $ cat test.php
<?php
echo json_encode(['key'=>'ыфва']), PHP_EOL;
echo json_encode(['key'=>'ыфва'], JSON_UNESCAPED_UNICODE), PHP_EOL;
/tmp $ php test.php
{"key":"\u044b\u0444\u0432\u0430"}
{"key":"ыфва"}
/tmp $ php --version
PHP 7.3.6 (cli) (built: May 31 2019 23:38:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.6, Copyright (c) 1998-2018 Zend Technologies
with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans
with Zend OPcache v7.3.6, Copyright (c) 1999-2018, by Zend Technologies
You should use $_POST to get the form data like as below
header("Content-type: application/json; charset=utf-8");
file_put_contents('export.json', json_encode($_POST) );
Try this (source) :
header("Content-type: application/json; charset=utf-8")
$convert = file_put_contents('export.json', json_encode($_REQUEST));
$convert = iconv('CP1251', 'UTF-8', $convert);
Related
Suppose there are leading zeros in IPv4 address representation such as 012.012.012.012.
To suppress the leading zeros, I simply write the following code, and it works as I expected on my Mac:
% php -r 'var_dump(long2ip(ip2long("012.012.012.012")));'
Command line code:1:
string(11) "12.12.12.12"
% php --version
PHP 7.3.25 (cli) (built: Dec 25 2020 22:03:38) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.25, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.25, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v3.0.1, Copyright (c) 2002-2020, by Derick Rethans
But this simple piece of code doesn't work on CentOS 7, it seems like ip2long("012.012.012.012") returns false value:
$ php -r 'var_dump(long2ip(ip2long("012.012.012.012")));'
string(7) "0.0.0.0"
$ php --version
PHP 7.3.25 (cli) (built: Nov 24 2020 11:10:55) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.25, Copyright (c) 1998-2018 Zend Technologies
$ cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
Both PHP versions are identical and I would expect them to return exact same values. I also read the ip2long docs and tried to find if there is any special options/configurations for this behaviour, but no luck.
Could someone lead me in the right direction?
What makes them different? And what should I do?
OK, I found the culprit. Different implementation of inet_pton(3) between LLVM and GCC leads this behaviour, as Russ-san commented. There was a bug report about the exact same problem 6 years ago. It seems PHP v5.2.10 had started using inet_pton instead of inet_addr according to this patch, that's why older versions of PHP treat each chunks as octal.
test.c
#include <stdio.h>
#include <arpa/inet.h>
#define INADDR "012.012.012.012"
int main() {
struct in_addr inaddr;
if (inet_pton(AF_INET, INADDR, &inaddr) == 0) {
printf("Invalid: %s\n", INADDR);
}
else {
printf("Valid: %s\n", INADDR);
}
return 0;
}
Mac
% cc test.cc && ./a.out
Valid: 012.012.012.012
Linux
$ cc test.cc && ./a.out
Invalid: 012.012.012.012
I gave up using ip2long / long2ip built-in functions and now trying to use https://github.com/mlocati/ip-lib
>>> \IPLib\Address\IPv4::fromString("1.2.3.12")->toString(true);
=> "001.002.003.012"
>>> \IPLib\Address\IPv4::fromString("001.002.003.012")->toString();
=> "1.2.3.12"
No problems so far.
My php version:
➜ ~ php -v
PHP 7.3.1 (cli) (built: Jan 10 2019 13:16:34) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.1, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.1, Copyright (c) 1999-2018, by Zend Technologies
I dont understand why spaced value in function is_numeric behaves like this?
➜ ~ php -a
php > var_dump(is_numeric('0012432'));
bool(true)
php > var_dump(is_numeric(' 0012432'));
bool(true)
php > var_dump(is_numeric('0012432 '));
bool(false)
This function handles leading spaces differently than is_int(), is_float(), is_real(), is_long(), and is_double().
is_numeric(" 12345") = true
is_int(" 12345") = false
is_float(" 12345") = false
is_real(" 12345") = false
is_long(" 12345") = false
is_double(" 12345") = false
I found a bug report open for these cases, not sure if they will be working on this,
Bug report
So does every other part of the core PHP language that accepts numeric strings. It's an annoying inconsistency, but it is intentional and has been like this for many years. I am personally intending to fix this in a future version.
Currently i am working with Laravel and the maxcdn API.
This is my very little test code
Route::get('cdn', function() {
$api = new MaxCDN("myurl","mykey","mysecret");
$data = $api->get('/account.json');
return response()->json($data);
});
The header is set to json as you see
application/json
Now if i visit my /cdn url the Chrome JSON Formatter should be of course formate the output. But it does not work, i only get this output
"{\"code\":200,\"data\":{\"account\":{\"id\":\"12312\",\"name\":\"My Name \",\"alias\":\"myurl\",\"date_created\":\"2015-05-17 07:51:50\",\"date_updated\":\"0000-00-00 ........
I am working on my homestead machine
php -v
PHP 5.6.6-1+deb.sury.org~utopic+1 (cli) (built: Feb 20 2015 11:25:37)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
The $api->get('/account.json'); call returns a string that contains the json data. You are then passing this string to the response()->json(); method, and that is the output you are seeing.
You need to run the string through json_decode() in order to convert the json string into a PHP object. Once you do that, you can pass that result to the response()->json(); method, and you will get the result you're looking for.
Route::get('cdn', function() {
$api = new MaxCDN("myurl","mykey","mysecret");
$stringData = $api->get('/account.json');
// convert the json string into an actual PHP object
$data = json_decode($stringData);
// respond with the object
return response()->json($data);
});
I have the strangest bug. I was like wtf^12.
I have a script wchich calls following function:
function checkNewFilename($newFilename)
{
if (file_exists($newFilename)) {
if (!unlink($newFilename)) {
debugDeploy($newFilename . ' already exists and could not be deleted.');
throw new Exception($newFilename . ' already exists and could not be deleted.');
}
}
}
It gets used like this:
$newFilename = $defaultConfig . DIRECTORY_SEPARATOR . 'client.xml';
checkNewFilename($newFilename);
echo 'wtf is happening here';
copy($configFilename, $newFilename);
Now when i run this code with normall browser/xdebug i get a cachable fatal error as it should be. BUT when i run the script via shell on
php script.php param param param
Somthing nuts happens. IT ignores the exceptions and gous as nothing happend. I get the error before the exception AND the echo after the function call... wth.
I am runing on
PHP 5.3.3-7+squeeze14 with Suhosin-Patch (cli) (built: Aug 6 2012
14:18:06) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0,
Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH
Anyone has any idea what causes this strange behavior?
I am using Mac, netbeans, x-debug, with Mongo PHP. I am trying to run a basic search like:
$results = $mongo->$col->find();
However during debug, the value for $results is slaveOkay = 0 and timeout = 30000. Why is this so? Some version information about the tools installed are:
PHP 5.4.20 (cli) (built: Sep 24 2013 10:10:10) (DEBUG) Copyright (c)
1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013
Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
MongoDB shell version: 2.4.6
Thanks.
Where does that $col variable come from?
And where does $mongo come from?
The correct way to query a collection is
<?php
$mongo = new MongoClient(...);
$collection = $mongo->databaseName->collectionName;
$results = $collection->find();
?>
Then you can iterate over $results like so:
foreach($results as $document) {
/* Do something with $document */
}
See http://php.net/mongocollection.find and http://php.net/mongo.queries for more details