php json_encode(): "Malformed UTF-8 characters" despite no unusual characters - php

I have a script which creates an array containing entries for a table that I'd like to encode as json to send to my app. Each entry is an associative array of k/v pairs of string=>string|int. The code to generate the full array is as follows:
$entries = [];
foreach($stats as $i => $stat){
$playerStats = new Player_Stats($stat);
$entry = [
'place' => $i,
'name' => $playerStats->name(),
'elo' => $playerStats->currentRating(),
'highest' => $playerStats->highRating(),
'masterPoints' => $playerStats->onlineMasterPoints(),
'winLose' => $playerStats->matchesWonLost(),
'winRate' => $playerStats->winPercent(),
'numEvents' => $playerStats->numEvents(),
'wins' => $playerStats->eventsWon(),
'placed' => $playerStats->eventsPlaced()
];
$entries[] = $entry;
}
$result = json_encode($entries);
When I do a count or vardump etc on entries, It clearly shows that its been properly filled with the correct data. json_encode() returns false however. Using json_last_error_msg(), I get the UTF-8 error in the title: Malformed UTF-8 characters, possibly incorrectly encoded. All of the other posts on this issue I could find involved characters from other languages. All of the content in this array is made of the english alphabet and numbers, all ascii characters let alone utf-8.
Am I just missing something trivial (I usually am) or should I be looking for or trying something else completely?

You could use the option to ignore these, JSON_INVALID_UTF8_IGNORE so your code would be json_encode($data, JSON_INVALID_UTF8_IGNORE);
This will ignore invalid UTF-8 as the name describes. You can see all options for json_encode here. https://www.php.net/manual/en/function.json-encode.php

Something like this could do the trick:
$result = json_encode(array_map(utf8_encode, $entries))

Related

PHP retry value to string

I have this problem:
One string like:
$stringa = "array('name' => 'John')"
I want obtain : array('name' => 'John') for use in my code like array
Any helps? Thanks
Caution using eval...
Caution The eval() language construct is very dangerous because it
allows execution of arbitrary PHP code. Its use thus is discouraged.
If you have carefully verified that there is no other option than to
use this construct, pay special attention not to pass any user
provided data into it without properly validating it beforehand.
<?php
$stringa = "array('name' => 'John')";
$code = "\$a = " . $stringa . ";";
eval($code);
print_r($a);
Gouda Elalfy was on the right idea, but his solution simply made the whole string a single array value.
First, we need to remove the excess datatype information:
$slimString = substr($stringa,6);
$slimString = rtrim($slimString,")");
This now gives us a string of:
'name' => 'John'
So then the Keys and the values in the string need to be split up,
so break at => as so:
For multiple values in the string:
This method also includes the single quotes to limit catching punctuation commas (please note this method was screwed up by trim not being as effective as I'd have liked and requiring str_replace quotes instead).
$slimString = str_replace("', '","=>", $slimString);
Then
$slimStringParts = explode("=>", $slimString);
This will split on => (or ,) so that multiple values of array contents can be generated.
Then cycle through each of the array pieces, on the basis that the EVEN numbers (and zero) are the Keys and the ODD numbers are the values, also removing the quotes as well for each one,
I was originally using trim but for some reason trim was not working as fully as I expected. so instead reverted to st_replace
foreach($slimStringParts as $key => $part){
if(($key%2) == 0){
$part = str_replace("'","",$part);
$arrayOutput[$part] = str_replace("'","",$slimStringParts[$key+1]);
}
}
unset($key, $part);
The foreach only acts upon the even and zero values as referenced above, and they take the original key value + 1 as their contents.
The trim/str_replace removes the single quotes and looks untidy but this works for a string of one or more array values
And finally the Output:
print_r($arrayOutput);
Test with the original:
input : "array('name' => 'John')"
Output : Array ( [name ] => John )
Tested with a multivalue array string:
input : "array('name' => 'John', 'surname' => 'James', 'Ride' => 'horse')"
Output : Array ( [name ] => John [surname ] => James [Ride ] => horse )
Full code:
$stringa = "array('name' => 'John')";
$stringb = "array('name' => 'John', 'surname' => 'James', 'Ride' => 'horse')";
$slimString = substr($stringb,6);
$slimString = rtrim($slimString,")");
$slimString = str_replace("', '","=>", $slimString);
$slimStringParts = explode("=>", $slimString);
foreach($slimStringParts as $key => $part){
if(($key%2) == 0){
$part = str_replace("'","",$part);
$arrayOutput[$part] = str_replace("'","",$slimStringParts[$key+1]);
}
}
unset($key,$part);
print_r($arrayOutput);
exit;
Please note my trim() idea was what I wanted but that seems to be influenced by my page character encoding :-(
You can use eval() function.
But it's not recommanded.
http://php.net/manual/en/function.eval.php
Eval is unsafe, try to use it as temporary method and find a better solution.
Example :
$stringa="array('name' => 'John');";
$array = eval($stringa);

Converting array to JSON string when array already contains JSON strings

I have an array that contains a JSON string.
Array
(
[0] => Array
(
[name] => Original
[nutrients] => {"calories":{"value":2500,"operator":2},"protein":{"value":500,"operator":1},"carbs":{"value":200,"operator":0},"fat":{"value":50,"operator":0},"sugar":{"value":1,"operator":2}}
)
[1] => Array
(
[name] => Rest
[nutrients] => {"calories":{"value":5000,"operator":2},"sugar":{"value":10,"operator":2}}
)
)
I want to turn the whole array into a JSON string
echo json_encode($array);
But this throws a \ in front of all quotes
[{"name":"Original","nutrients":"{\"calories\":{\"value\":2500,\"operator\":2},\"protein\":{\"value\":500,\"operator\":1},\"carbs\":{\"value\":200,\"operator\":0},\"fat\":{\"value\":50,\"operator\":0},\"sugar\":{\"value\":1,\"operator\":2}}"},{"name":"Rest","nutrients":"{\"calories\":{\"value\":5000,\"operator\":2},\"sugar\":{\"value\":10,\"operator\":2}}"}]
This problem comes about because the nutrients value is already a JSON string.
How can I convert an array into a JSON string when it already contains JSON strings, while having no slashes in front of the quotes?
Use json_decode to convert 'nutrients' to array.
foreach($array as &$a){
$a['nutrients'] = json_decode($a['nutrients']);
}
Then
echo json_encode($array);
How can I convert an array into a JSON string when it already contains JSON strings, while having no slashes in front of the quotes?
If you want to preserve the JSON values as strings; then, you can't, and you shouldn't be able to!
If your array already contains some JSON values (in which they'll have some quotation marks: ") and you want to encode that array into a JSON string, then the quotation marks must be properly escaped, what you get correctly; otherwise, the entire JSON string will be corrupt because of miss-quotation-mark matches.
That's because the " has a special meaning in JSON, but the \" means the "double quotation mark character" not the special token of "; for example, removing the backslashes from the valid JSON string causes some syntax errors for sure:
$json = '[{"name":"Original","nutrients":"{\"calories\":{\"value\":2500,\"operator\":2},\"protein\":{\"value\":500,\"operator\":1},\"carbs\":{\"value\":200,\"operator\":0},\"fat\":{\"value\":50,\"operator\":0},\"sugar\":{\"value\":1,\"operator\":2}}"},{"name":"Rest","nutrients":"{\"calories\":{\"value\":5000,\"operator\":2},\"sugar\":{\"value\":10,\"operator\":2}}"}]';
$json_noBackslashes = str_replace('\\', '', $json);
$json_decoded = json_decode($json_noBackslashes);
echo json_last_error_msg(); // Syntax error
You should be able to json_decode the json data first, then put it back to the original array. After that, you can encode the whole array again to get the desired output.
foreach ($data as $datum) {
$data['nutrients'] = json_decode($data['nutrients']);
}
json_encode($data);

How can i convert this string to array?

I am stucked with converting the string of specific format to array. Spliting the string using explode doesn't seems to be the right approach and i am not so good with regular expressions. So my question is how can i convert the following string to array?
Current format of the string
maxWidth: 800,
openEffect: elastic,
closeEffect: elastic,
helpers : {
title : {
type: outside
},
thumbs : {
width : 50,
height : 50
}
}
Desired Array
array(
'maxWidth' => 800,
'openEffect' => 'elastic',
'closeEffect' => 'elastic',
'helpers' => array(
'title' => array('type' => 'outside'),
'thumbs' => array('width' => 50, 'height' => 50)
)
)
Any help would be greatly appreciated.
EDIT BASED ON RESPONSES:
The string looks like a JSON but it is not a JSON. Its just a string input from user in that format. The input will be from normal user so i want to keep it simple. There is minimum chance that the normal user will enter a valid JSON.
The string in your example is almost a valid JSON (JavaScript Object Notation) structure!
Here's what your string would look like as valid JSON
{
"maxWidth": 800,
"openEffect": "elastic",
"closeEffect": "elastic",
"helpers": {
"title": {
"type": "outside"
},
"thumbs": {
"width": 50,
"height": 50
}
}
}
So our approach (as suggested by #WiseGuy) would be to first inject a few characters with preg_replace to Turn your string init into valid JSON:
$str = preg_replace('/\b/' , '"' , $str);
$str = '{' . $str . '}';
The regex above is using the Word Boundaries anchor to add quotation marks around all words. Then we wrap the whole thing in curly braces and voilĂ , we've got a x-language compatible object format.
We can now use a standard function to produce our object:
$objUserConfig = json_decode($str, true);
A good beginners tutorial on JSON here: http://code.drewwilson.com/entry/an-introduction-to-json
Use a linter tool such as http://jsonlint.com/ to validate JSON. I used it to debug your example and convert it into proper JSON for my example.
Your input string looks like a json format. PHP has json_decode() to convert json string to object.
To convert to array, use below code:
json_decode($jsonStr, true);
Refer: http://php.net/manual/en/function.json-decode.php
Edit: I know you are showing the printed output of an array and
not a static representation of a php declared array. This is just an
example of how you might convert it into something that can be parsed
into that array. If php has that ability to do so dynamically (I don't know).
Convert the file, read in $str
In this order, do regex on $str.
Each is global flag.
(?i)([a-z]+) to '$1'
(?i)(?<=[a-z]')\s*:(?=\s*[^{\s]) to =>
(?i)(?<=[a-z]')\s*:\s*{ to => array(
} to )
Finally, $newstr = "array(\n$str\n)"
However, something like this that can be read by a php parser as a static
array. How it gets dynamically interpreted into vars I don't know.
Perl can do this.

RegEx for hashtag separated string

I have bunch of strings like this:
a#aax1aay222b#bbx4bby555bbz6c#mmm1d#ara1e#abc
And what I need to do is to split them up based on the hashtag position to something like this:
Array
(
[0] => A
[1] => AAX1AAY222
[2] => B
[3] => BBX4BBY555BBZ6
[4] => C
[5] => MMM1
[6] => D
[7] => ARA1
[8] => E
[9] => ABC
)
So, as you see the character right behind the hashtag is captured plus everything after the hashtag just right before the next char+hashtag.
I've the following RegEx which works fine only when I have a numeric value in the end of each part.
Here is the RegEx set up:
preg_split('/([A-Z])+#/', $text, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
And it works fine with something like this:
C#mmm1D#ara1
But, if I change it to this (removing the numbers):
C#mmmD#ara
Then it will be the result, which is not good:
Array
(
[0] => C
[1] => D
)
I've looked at this question and this one also, which are similar but none of them worked for me.
So, my question is why does it work only if it has followed by a number? and how I can solve it?
Here you can see some of them sample strings which I have:
a#123b#abcc#def456 // A:123, B:ABC, C:DEF456
a#abc1def2efg3b#abcdefc#8 // A:ABC1DEF2EFG3, B:ABCDEF, C:8
a#abcdef123b#5c#xyz789 // A:ABCDEF123, B:5, C:XYZ789
P.S. Strings are case-insensitive.
P.P.S. If you ever thinking what the hell are these strings, they are user submitted answers to a questionnaire, and I can't do anything on them like refactoring as they are already stored and just need to be proceed.
Why Not Using explode?
If you look at my examples you will see that I need to capture the character right before the # as well. If you think it's possible with explode() please post the output as well, thanks!
Update
Should we focus on why /([A-Z])+#/ works only if numbers included? thanks.
Instead of using preg_split(), decide what you want to match instead:
A set of "words" if followed by either <any-char># or <end-of-string>.
A character if immediately followed by #.
$str = 'a#aax1aay222b#bbx4bby555bbz6c#mmm1d#ara1e#abc';
preg_match_all('/\w+(?=.#|$)|\w(?=#)/', $str, $matches);
Demo
This expression uses two look-ahead assertions. The results are in $matches[0].
Update
Another way of looking at it would be this:
preg_match_all('/(\w)#(\w+)(?=\w#|$)/', $str, $matches);
print_r(array_combine($matches[1], $matches[2]));
Each entry starts with a single character, followed by a hash, followed by X characters until either the end of the string is encountered or the start of a next entry.
The output is this:
Array
(
[a] => aax1aay222
[b] => bbx4bby555bbz6
[c] => mmm1
[d] => ara1
[e] => abc
)
If you still want to use preg_split you can remove the + and it might work as expected:
'/([A-Z])#/i'
Since then you only match the hashtag and ONE alpha character before, and not all them.
Example: http://codepad.viper-7.com/z1kFDb
Edit: Added a case-insensitive flag i in the pattern.
Use explode() rather than Regexp
$tmpArray = explode("#","a#aax1aay222b#bbx4bby555bbz6c#mmm1d#ara1e#abc");
$myArray = array();
for($i = 0; $i < count($tmpArray) - 1; $i++) {
if (substr($tmpArray[$i],0,-1)) $myArray[] = substr($tmpArray[$i],0,-1);
if (substr($tmpArray[$i],-1)) $myArray[] = substr($tmpArray[$i],-1);
}
if (count($tmpArray) && $tmpArray[count($tmpArray) - 1]) $myArray[] = $tmpArray[count($tmpArray) - 1];
edit: I updated my answer to reflect better reading the questions
You can use explode() function that will split the string except the hash signs, like stated in the answers given before.
$myArray = explode("#",$string);
For the string 'a#aax1aay222b#bbx4bby555bbz6c#mmm1d#ara1e#abc' this returns something like
$myarray = array('a', 'aax1aay22b', 'bbx4bby555bbz6c' ....);
All you need now is to take the last character of each string in array as another item.
$copy = array();
foreach($myArray as $item){
$beginning = substr($item,0,strlen($item)-1); // this takes all characters except the last one
$ending = substr($item,-1); // this takes the last one
$copy[] = $beginning;
$copy[] = $ending;
} // end foreach
This is an example, not tested.
EDIT
Instead of substr($item,0,strlen($item)-1); you might use substr($item,0,-1);.

php convert string with new lines into array?

I am getting data from an API and the resulting string is
[RESPONSE]
PROPERTY[STATUS][0]=ACTIVE
PROPERTY[REGISTRATIONEXPIRATIONDATE][0]=2012-04-04 19:48:48
DESCRIPTION=Command completed successfully
QUEUETIME=0
CODE=200
RUNTIME=0.352
QUEUETIME=0
RUNTIME=0.8
EOF
I am trying to convert this into an array like
Array(
['PROPERTY[STATUS][0]'] => ACTIVE,
['CODE'] => 200,
...
);
So I am trying to explode it using the resulting file_get_content function with an explode like
$output = explode('=',file_get_contents($url));
But the problem is the returning values are not always returned in the same order, so I need to have it like $array['CODE'] = 200, and $array['RUNTIME'] = 0.352 however there does not seem to be any kind of new line characters? I tried \r\n, \n, <br>, \r\n\r\n in the explode function to no avail. But there is new lines in both notepad and the browser.
So my question is there some way to determine if a string is on a new line or determine what the character forcing the new line is? If not is there some other way I could read this into an array?
To find out what the breaking character is, you could do this (if $data contatins the string example you've posted):
echo ord($data[strlen('[RESPONSE]')]) . PHP_EOL;
echo ord($data[strlen('[RESPONSE]')+1]); // if there's a second char
Then take a look in the ASCII table to see what it is.
EDIT: Then you could explode the data using that newly found character:
explode(ord($ascii_value), $data);
Btw, does file() return a correct array?
Explode on "\n" with double quotes so PHP understands this is a line feed and not a backslashed n ;-) then explode each item on =
Why not just use parse_ini_file() or parse_ini_string()?
It should do everything you need (build an array) in one easy step.
Try
preg_split("/$/m", $str)
or
preg_split("/$\n?/m", $str)
for the split
The lazy solution would be:
$response = strtr($response, "\r", "\n");
preg_match_all('#^(.+)=(.+)\s*$#m', $response, $parts);
$parts = array_combine($parts[1], $parts[2]);
Gives you:
Array (
[PROPERTY[STATUS][0]] => ACTIVE
[PROPERTY[REGISTRATIONEXPIRATIONDATE][0]] => 2012-04-04 19:48:48
[DESCRIPTION] => Command completed successfully
[QUEUETIME] => 0
[CODE] => 200
[RUNTIME] => 0.8

Categories