Special characters and New line in php to JSON - php

I want to print a clean JSON response with all special characters displayed, paragraphs, line breaks etc.. I am using this function to perform all necessary operations and also clean up the response regarding bold text, etc.. basically, all I need is to do add a blank line in the response each time \r is detected in the text, for this specific JSON response. Is there a way to do it?
function retiraTagHTML($textoComTag){
$textoComTag = html_entity_decode($textoComTag);
$textoComTag = preg_replace('/\n{2,}/', "</p><p>", trim($textoComTag));
$textoComTag = preg_replace('/\n/', '<br>',$textoComTag);
return strip_tags($textoComTag, '<(.*?)>');
}

This ended up doing what I needed
function removeHTMLtag($textWithTag){
$textWithoutTag = strip_tags($textWithTag, '<(.*?)>');
$textWithoutTag = html_entity_decode($textWithoutTag );
return $textWithoutTag ;
}

Related

Receive JSON POST in PHP with ENTER in one of the field value

When "descriptions" field has "enter" (newline) API is failing.
Image to check all parameter sent by users
Below code to get the data from posted JSON.
// get posted data
$jason_value = json_decode(file_get_contents("php://input"));
$crm_id = $jason_value->data->crmId;
$descriptions = $jason_value->data->descriptions;
I would like to accept descriptions as a string one line.
descriptions = "10+ windows modern style 7057655959".
I do not have access to the program where the user enters the description where I can add validation and convert it to \n.
Getting below string after conversion
{ "jwt": "eyJ0", "data": { "crmId": "15876047", "geoconceptAppointmentId": "15876","geoconceptCustomerId": "15876047","status": "Rejected","appointmentDateTime": "","firstName": "Nick Test","lastName": "PA","address": "9112 RUE Tom","city": "MONTREAL","state": "QC","zip": "H2N1T1","country": "CAN","phoneNumber1": "5148332222","phoneNumber2": "5148332222","email": "nbskgg#gmail.com","dateEntry": "2019-06-20 12:02","dateModify": "2019-06-20 12:02","preferredWayToContact": "","textMsgFlag": "Y","hearAboutUs": "Referral","perferredTime": "Anytime","descriptions": "I have to call at 5" pm. ","worklog": "This is the comment ","rejectReason": "Area | Region","referredByDC": "09999","referredByStoreUsername": "store215","assignedUsername": "","createdByUsername": "np","modifiedByUsername": "np","btgMarket": "Montreal"}}
You're correct that in PHP7+, a literal tab or newline is going to cause the json parsing to fail. file_get_contents("php://input") returns a single string so I see no reason why you couldn't just filter that before you attempt to parse it. But maybe I'm missing something.
//Catch Unix OR DOS line endings, but not both
$filter = Array("\n","\n\r");
$replace = " ";
$cleanJSON = str_replace($filter, $replace, file_get_contents("php://input");
$data = json_decode($cleanJSON));
I want to point out that after this point, your code is referencing a variable that does not exist: $jason_value
$crm_id = $jason_value->data->crmId;
$descriptions = $jason_value->data->descriptions;
To reference properties of the object you just created, go directly to $data:
$crm_id = $data->crmId;
$descriptions = $data->descriptions;
I expect that you'd want to replace the newline with a space but you may just want an empty string if what you're actually encountering has a space before the newline but that's impossible to tell from what we have.

PHP & JSON : Trying to write JSON to Mysql

I am having an issue where my PHP script opens a file with JSON code and needs to insert it into a MySQL database.
For some reason it only displays some of the output from the JSON.
Here is my code
$json = json_decode(file_get_contents('data.json'), true);
$data = $json;
// VAR's
$system = $data['System'];
$cid_from = $data["From"];
$cid_to = $data['To'];
//DEBUG USAGES
$array = print_r($data, true);
////// THIS ONE WORKS FINE
echo $data["System"];
////// THIS ONE DOESN'T WORK
echo $data["To"];
file_put_contents('output/json-local.txt',$array . "\r\n", FILE_APPEND);
////// BUT HERE IT ACTUALLY WORKS
file_put_contents('output/cli-from.txt',$data['From']. "\r\n", FILE_APPEND);
file_put_contents('output/cli-to.txt',$data['To']. "\r\n", FILE_APPEND);
// file_put_contents('json-sysid-local.txt',$systemid . "\r\n", FILE_APPEND);
Here is the contents of data.json
{"action":"call-data-record",
"System":"48130b83e2232f0ecd366a92d4d1261d",
"PrimaryCallID":"n1bWEfCdHcf#MSS.MTN.CO.ZA-b2b_1",
"CallID":"0440b807#pbx",
"From":"<sip:+27722080036#xxx.co.za>",
"To":"<sip:27102850816#xxx.co.za>",
"Direction":"O",
"RemoteParty":"",
"LocalParty":"",
"TrunkName":"",
"TrunkID":"",
"Cost":"",
"CMC":"",
"Domain":"xxx.co.za",
"TimeStart":"2018-08-14 16:03:21",
"TimeConnected":"",
"TimeEnd":"2018-08-14 16:03:23",
"LocalTime":"2018-08-14 18:03:21",
"DurationHHMMSS":"0:00:00",
"Duration":"0",
"RecordLocation":"",
"RecordUsers":"",
"Type":"hunt",
"Extension":"100",
"ExtensionName":"100",
"IdleDuration":"",
"RingDuration":"2",
"HoldDuration":"0",
"IvrDuration":"0",
"AccountNumber":"400",
"IPAdr":"",
"Quality":"VQSessionReport: CallTerm\r\nLocalMetrics:\r\nCallID:0440b807#pbx\r\nFromID:<sip:27102850816#xxx.co.za>\r\nToID:<sip:+27722080036#xxxx.co.za>;tag=1460166964\r\nx-UserAgent:Vodia-PBX/57.0\r\nx-SIPmetrics:SVA=RG SRD=91\r\nx-SIPterm:SDC=OK SDR=OR\r\n"}
Your "To" data is encapsulated in <>. This causes your browser to interpret it as an HTML tag and not display any content.
You can (should!) escape the special HTML control characters:
echo htmlspecialchars($data["To"]);
See http://php.net/htmlspecialchars
Edit: It doesn't hurt to precautionary add this to your other outputs aswell. If the string doesn't contain such characters, it will simply be returned onchanged. You eliminate possible XSS attack vectors this way.
The browser source clearly shows "To":"" is being written by PHP to the browser output correctly but the browser is interpreting as an HTML opening tag hence ignoring the rest of the content.
Wrap your output in the PHP htmlspecialchars() function to see the output as in the file.
Add - echo "TO : ".htmlspecialchars($data["To"]);

newline in json_encode() output

I am building an output array like so
if (count($errors)) {
$success = 'false';
$output['json_msg'] = "Please try your submission again.";
$output['errors'] = $errors;
} else {
$success = 'true';
$output['json_msg'] = "Thanks for Becoming a NOLA Insider!";
}
$output['success'] = $success;
header('Content-type:application/json;charset=utf-8');
if (count($errors)) { http_response_code(500); }
echo json_encode($output);
exit;
But when I look at the response in Chrome's Network pane of the developer tools I see what appears to be a newline in response:
I tried wrapping json_encode() in trim() but this gave garbled output.
How do I eliminate the carriage return?
You can try to remove new line using str_replace
$output = str_replace(array("\r\n", "\n", "\r"),'',$output);
echo json_encode($output);
Do you have a ?> at the end of your PHP file and what's happening when you remove it ?
Because you may have a carriage return at the end of the script which may be sent before your response :
?>\n
// END OF FILE
This is explained by the fact that PHP is actually a templating language :
Here is a file which defines a function and which displays a text :
<?php
/**
* #File lib.php
*/
function sayHello()
{
echo "hello";
}
?>
forgotten text
And here is a file that includes this file.
<?php
/**
* #file index.php
*/
include_once('lib.php');
sayHello();
This will output :
forgotten text
hello
The "forgotten text" is output when the lib.php file is included whereas the "hello" is output after.
(But it may be even simpler and just the point that #nanocv suggested)
If you are getting the new line characters like \r\n to your json code after json_encode() you can follow up the method with the final json_value that you get. This will remove up all the new lines that has been output-ed from the code that you obtain after you perform the json_encode().
Hence you need to preg_replace() the json outputed value as follows which will remove uo the new lines from the json_code.
This will replace the new lines with no value over to the second parameter in preg_replace().
Try not to provide any white spaced between the php codes (i.e) Opening and Closing Codes that you process either at the beginning or at the end of the document. This may cause the issue sometimes.
Code:
$output_json = preg_replace("!\r?\n!","", $output_json);
I bet your php code starts this way:
1. <--- Note the blank line here
2. <?php
That's a new line character that will became part of the result.
(This way I could recreate the same behavior)
I solved by removing spaces from the php file indicated in the include .

Formatting JSON formatted text file in PHP

So I got a HTML page with a button. When I click the button, a separate javascript file sends a GET request to my PHP file, expecting a JSON object in return. My PHP reads a JSON formatted text file and should convert it into a JSONObject and echo it out for my javascipt. I had some code working before, but it doesn't seem to do it anymore since I changed to a Ajax aproach instead of having everything in the same file. This is my code:
readLog.php
<?php
class test{
function clean($string){
return json_decode(rtrim(trim($string),','),true);
}
function getLog(){
header('Content-Type: application/json');
$logLines = file('../../../home/shares/flower_hum/humid.log');
$entries = array_map("clean",$logLines);
$finalOutput = ['log' => $entries];
echo json_encode($logLines);
}
}
?>
My humid.log file looks like this:
{"date":"26/09/2016", "time":"22:40:46","temp":"16.0", "humidity":"71.0" }
{"date":"26/09/2016", "time":"23:10:47","temp":"16.0", "humidity":"71.0" }
Now If I press my button, this is the response I get checking the console in my web browser:
Response:
["{\"date\":\"26\/09\/2016\", \"time\":\"22:40:46\",\"temp\":\"16.0\", \"humidity\":\"71.0\" }{\"date\":\"26\/09\/2016\", \"time\":\"23:10:47\",\"temp\":\"16.0\", \"humidity\":\"71.0\" }\n"]
JSON:
"{"date":"26/09/2016", "time":"22:40:46","temp":"16.0", "humidity":"71.0" }{"date":"26/09/2016", "time":"23:10:47","temp":"16.0", "humidity":"71.0" }\n"
obviously something is wrong with the formatting, but I don't know what. As I said, this code worked just fine when I had my php and HTML in the same file.
EDIT:
I have also tried formatting the JSON with something like this, but it just prints the brackets:
function getLog(){
$text = file('../../../home/shares/flower_hum/humid.log');
$textRemoved ="["; //Add opening bracket.
$textRemoved .= substr($text, 0, strlen($text)-1); (Remove last comma)
$textRemoved .="]";//Add closing bracket
$json = json_encode($textRemoved);
echo $json;
}
So I managed to solve it myself. Basicly The formatting of the textfile was wrong and as some commentors said, I don't need to encode it if I am doing it myself. What I ended up doing was in my application that generates the log file to add comma after each row. Then in my PHP I added brackets and removed the last comma.
function getLog(){
header('Content-Type: application/json');
$file = file_get_contents('../../../home/shares/flower_hum/humid.log');
$lengthOfFile = strlen($file)-2;
$subFile = substr($file, 0, $lengthOfFile);
$res ="[";
$res .= $subFile;
$res .="]";
echo $res;
}
You can't just jam two+ JSON strings togther. It's JSON, which means it HAS to be syntactically correct Javascript CODE. If you want to join two json strings together, you HAVE to decode them to a native data structure, join those structures together, then re-encode the new merged structure:
$temp1 = json_decode('{"foo":"bar"}', true);
$temp2 = json_decode('{"baz":"qux"}', true);
$new = array_merge($temp1, $temp2);
echo json_encode($new);
which will produce:
{"foo":"bar","baz":"qux"}
and remain valid JSON/Javascript.
Why? Consider that bare integers are valid json:
json_encode(42) -> 42
json_encode(123) -> 123
If you have two json-encoded integers and jam together, you get a "new" integer:
42123
and on the receiving end, you'll be going "Ok, so where is the split between the two", because 4 and 2123 are just as valid as possible original values as 4212 and 3.
Sending the two integers as distinct and SEPARATABLE values would require an array:
[42,123]

JSON character encoding in php script

I am retrieving json from a php file that connects to the database and then creates the json to be imported into my page via an ajax call.
Some of the database columns have file paths in them, ie images/myfolder/myfile
The json that I see when I open the page in a web browser formats the file path like this:
\/images\/myfolder\/myfile
Are the escaped characters going to break the json?
i.e, if i do var icon_image = myData.file
will icon_image hold: \/images\/myfolder\/myfile or /images/myfolder/myfile
I am hoping the second option from above, but if it doesn't how do I get it to display as /images/myfolder/myfile
I have put mb_internal_encoding( 'UTF-8' ); at the top of the php page
The script that generates the json is as follows:
mb_internal_encoding( 'UTF-8' );
mysql_select_db($database_growth_conn, $growth_conn);
$query_rs_icons = sprintf("SELECT * FROM icons_ico ORDER BY name_ico");
//echo($query_rs_icons);
$rs_icons = mysql_query($query_rs_icons, $growth_conn) or die(mysql_error());
$row_rs_icons = mysql_fetch_assoc($rs_icons);
$totalRows_rs_icons = mysql_num_rows($rs_icons)
$rows = array();
while($r = mysql_fetch_assoc($rs_icons)) {
$rows[] = $r;
}
$jsondata = json_encode($rows);
echo '{"icons":'.$jsondata.'}';
are the escaped characters going to break the json?
No. A backslash-escaped non-special punctuation is just the same as the punctuation itself. The JS string literals "a/b" and "a\/b" result in the same string value, a/b.
json_encode escapes the forward slash character for you so that if you try to include a string with the sequence </script> in it in a script element, it doesn't prematurely end the script block.

Categories