Json_decode with special chars Json_decode with special chars codeigniter codeigniter

Json_decode with special chars


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*:/','$1"$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)


$string = preg_replace("/[\r\n]+/", " ", $string);$json = utf8_encode($string);$json = json_decode($json);var_dump($json);