The manual for json_encode specifies this:
All string data must be UTF-8 encoded.
Thus, try array_mapping utf8_encode() to your array before you encode it:
Copy$arr = array_map('utf8_encode', $arr);
$json = json_encode($arr);
// {"funds":"ComStage STOXX\u00c2\u00aeEurope 600 Techn NR ETF"}
For reference, take a look at the differences between the three examples on this fiddle. The first doesn't use character encoding, the second uses htmlentities and the third uses utf8_encode - they all return different results.
For consistency, you should use utf8_encode().
Docs
- json_encode()
- utf8_encode()
- array_map()
The manual for json_encode specifies this:
All string data must be UTF-8 encoded.
Thus, try array_mapping utf8_encode() to your array before you encode it:
Copy$arr = array_map('utf8_encode', $arr);
$json = json_encode($arr);
// {"funds":"ComStage STOXX\u00c2\u00aeEurope 600 Techn NR ETF"}
For reference, take a look at the differences between the three examples on this fiddle. The first doesn't use character encoding, the second uses htmlentities and the third uses utf8_encode - they all return different results.
For consistency, you should use utf8_encode().
Docs
- json_encode()
- utf8_encode()
- array_map()
Your input has to be encoded as UTF-8 or ISO-8859-1.
http://www.php.net/manual/en/function.json-encode.php
Because if you try to convert an array of non-utf8 characters you'll be getting 0 as return value.
Since 5.5.0 The return value on failure was changed from null string to FALSE.
You are having error because of new line in your string
$string = '{"projectnumber" : "4444","projecdescription" : "4444", "articles" : [{"position":1, "article_id" : 676, "online_text" : "### Behälter; Band I-III nach indiv. Stückliste, Sprache: DE
- Sprache: de"},{"position":2, "article_id" : 681, "online_text" : "### Behälter; Band I-III nach indiv. Stückliste, Sprache: ###
- Sprache: en"}]}';
$string = preg_replace("/[\r\n]+/", " ", $string);
$json = utf8_encode($string);
$json = json_decode($json);
var_dump($json);
Output
object(stdClass)[1]
public 'projectnumber' => string '4444' (length=4)
public 'projecdescription' => string '4444' (length=4)
public 'articles' =>
array
0 =>
object(stdClass)[2]
public 'position' => int 1
public 'article_id' => int 676
public 'online_text' => string '### Behälter; Band I-III nach indiv. Stückliste, Sprache: DE - Sprache: de' (length=78)
1 =>
object(stdClass)[3]
public 'position' => int 2
public 'article_id' => int 681
public 'online_text' => string '### Behälter; Band I-III nach indiv. Stückliste, Sprache: ### - Sprache: en' (length=79)
Voting for the newline too
json_decode_nice + keep linebreaks:
function json_decode_nice($json, $assoc = TRUE){
$json = str_replace("\n","\\n",$json);
$json = str_replace("\r","",$json);
$json = preg_replace('/([{,]+)(\s*)([^"]+?)\s*:/','
3":',$json);
$json = preg_replace('/(,)\s*}$/','}',$json);
return json_decode($json,$assoc);
}
If you want to keep the linebreaks just escape the slash.
You don't need utf-8 encode, if everything is set to utf-8 (header, db connection, etc)
The problem could be all character is UTF8, but json_encode does not handle it correctly.
here is a list of other characters (see Unicode characters list)
that will trigger the same error, so stripping off this one like (Å)
try below this could be help you.
htmlentities( (string) $value, ENT_QUOTES, 'utf-8', FALSE);
worth to check in this class Mage_Directory_Helper_Data
In billing.phtml file this code update state selection dropdown
var billingRegionUpdater = new RegionUpdater('billing:country_id', 'billing:region', 'billing:region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'billing:postcode');
So change in this function _getRegions($storeId) as suggested by Mr Liyakat
I have a website that lets the user build a dynamic form (think tabs with widgets) and then reduce the whole thing to a string with json.stringify().
But when I pass it to PHP (on one of our two systems to make things weirder) it adds a mess of escape characters. For example:
{\\"tab_type\\":\\"accordion\\",\\"options\\"{\\"tab::style\\":\\"color:0x8000ff00\\", When the correct formatting should look like:
"type":"accordion","options":{"tab::style":"color:0x8000ff00" The json is stringified properly on the JS side but it adds the slashes as soon as I look at the variable in the GET/POST on the PHP side.
Change your code to:
header('Content-Type: application/json; charset=utf-8', true,200);
$JSON["today"]=array();
for ($i=0; $i < count($olay_tarih); $i++) {
$gelen["date"]=array();
$gelen["content"]=array();
array_push($gelen["date"], $olay_date[$i]);
array_push($gelen["content"], $olay_content[$i]);
array_push($JSON["today"], $gelen);
}
$JSON = array_map('utf8_encode', $JSON);
echo json_encode($JSON);
Adding UTF-8 headers will make the browser recognize any special characters for this setting.
What's the output with this?
<?php
header('Content-Type: text/html');
echo(json_encode($JSON, JSON_UNESCAPED_UNICODE));
JSON_UNESCAPED_UNICODE
Encodes multibyte Unicode characters literally (default is to escape as \uXXXX). Available since PHP 5.4.0.