0

I am developing a ussd application and the api I am using only accepts json response in the following format

{
  "ispin": 1,
  "msisdn": "123456789",
  "partnerid": 10,
  "ShowInputBox": true,
  "displaystring": "content to be displayed"
  }

I have successfully worked on the php code that displays messages to users but cant add some formating to it. I want to include line breaks and hyperlinks. Can anyone identify any mistake in this code

<?php

$SESSION_ID = $_GET['SESSION_ID'];
$MSISDN = $_GET['MSISDN'];
$PartnerId = $_GET['PartnerId'];
$MNOID = $_GET['MNOID'];
$USSD_STRING = $_GET['USSD_STRING'];


$displaystring = 'Welcome to Polio Buddy. Please select.\n <a href="register.php">1 : Register Family</a>\n <a href="check.php">2: Check Vaccination Status</a>\n <a href="vaccinate.php">3: Vaccinate Child</a>';



$arr = array('ispin' => 0, 'msisdn' => $MSISDN, 'partnerid' => $PartnerId, 'ShowInputBox' => 'true', 'displaystring' => $displaystring);


echo json_encode($arr);

?>
nick
  • 333
  • 1
  • 4
  • 15
  • @barmar How can you mark this as duplicate? Look at the question again! I'm trying to line break the last parameter of the json response dude – nick Jan 18 '16 at 22:59
  • That's what "pretty printing" means -- display it nicely formatted. – Barmar Jan 18 '16 at 23:00

3 Answers3

0

Try converting your new line characters to <br> HTML elements, since whatever you use to parse and display the JSON is obviously not honouring \n new lines:

$displaystring = nl2br($displaystring);
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • this does not work. Have you tried running my entire script? – nick Jan 18 '16 at 22:35
  • @nick - I see, you aren't seeing the `\n`s propagate after having been put into the JSON. Adjusted answer. – scrowler Jan 18 '16 at 22:39
  • @nick please provide more detail about how the JSON is parsed and displayed. – scrowler Jan 18 '16 at 23:05
  • @nick haha - are you trying to format the JSON itself?? – scrowler Jan 18 '16 at 23:16
  • it just prints out the
    as it does with the \n new lines . It does not seem to honour both br and \n
    – nick Jan 18 '16 at 23:17
  • You can't format JSON with new lines or with HTML, that's not what its for. Its when you parse it and display it (in HTML) that you do that. JSON is not designed to be customer facing - the best you can do is what is outlined in the link that Barmar closed this question as a duplicate of – scrowler Jan 18 '16 at 23:18
0
$displaystring = "Welcome to Polio Buddy. Please select.\n <a href=\"register.php\">1 : Register Family</a>\n <a href=\"check.php\">2: Check Vaccination Status</a>\n <a href=\"vaccinate.php\">3: Vaccinate Child</a>";

PHP can interpret strings with double quotes or leave as it when you have single. my custom variable string print '\n\t $variable"; // will output unchanged \n\t $variable

BTW: Whenever it's possible it's better to use single quotes, as PHP don't waste resources for interpreting if there are any variables to output.

Jakub
  • 733
  • 5
  • 20
0

I am assuming that you are displaying "HTML" to the user and the formatting that you wish to show is in HTML, then \n wont cause new lines... use "<br/>".

And then your single and double quotes wont matter, either.

Lance
  • 638
  • 1
  • 6
  • 22