Find my Json code.
$result = {
"text_1":
[
{
"text": "Name
KarSho",
"left": 0,
"right": 0
}
]
}
I want "text" from this Json in PHP. I used following script,
$json = json_decode($result,true);
if($json && isset($json['text_1']))
{
$textblock =$json['text_1'][0];
echo $textblock['text'];
}
It's giving,
Name KarSho
But I want,
Name KarSho
What to do?
Well, to your surpeize, it will give you, Name KarSho
as output, But when you render that in HTML (In any Browser), You will not see a new line, because more than one spaces and new lines are ignored by browsers, if you go to view source of the browser, you will see a new line there,
If you want your HTML to show the new line, use
echo nl2br($textblock['text']);
So that your new lines will be converted to <br>
tag, and you will see that in your HTML output.
Edit:
If you want to see also in output (as is), You just want
echo json_encode($textblock['text']);
and to remove the quotes,
echo trim(json_encode($textblock['text']), '"');
<?php
header('Content-Type: text/plain');
$result = '{"text_1": [{"text": "Name
KarSho", "left": 0,"right": 0}]}';
$json = json_decode($result,true);
if($json && isset($json['text_1']))
{
$textblock =$json['text_1'][0];
echo $textblock['text'];
}
?>
use header function to disply next line
OP
doesn't want a new line, he wants to print also.
You can escape , for example:
$name = "John
Will";
echo str_replace("
", "\
", $name);
will output:
John
Will