I have a text file. I want to read the file and get some of datas from the element. While i read this file it may be return string (I am not sure).The file contains some data which like the follwing :
Can we get Prism Cluster Mail in summarize way like below.
----------------------------------------------------------------
{
"Employees":[
{
"userId":"rirani",
"jobTitleName":"Developer",
"firstName":"Romin",
"lastName":"Irani",
"preferredFullName":"Romin Irani",
"employeeCode":"E1",
"region":"CA",
"phoneNumber":"408-1234567",
"emailAddress":"[email protected]"
},
{
"userId":"nirani",
"jobTitleName":"Developer",
"firstName":"Neil",
"lastName":"Irani",
"preferredFullName":"Neil Irani",
"employeeCode":"E2",
"region":"CA",
"phoneNumber":"408-1111111",
"emailAddress":"[email protected]"
},
{
"userId":"thanks",
"jobTitleName":"Program Directory",
"firstName":"Tom",
"lastName":"Hanks",
"preferredFullName":"Tom Hanks",
"employeeCode":"E3",
"region":"CA",
"phoneNumber":"408-2222222",
"emailAddress":"[email protected]"
}
]
}
Best Regards
The Team
I want to extract userId,jobTitleName and phoneNumber. How can i do this? I am a new in php. I have tried by the folowing code. But it does not work properly. Can anyone please help ?
header('Content-type: application/json');
$data = preg_split("/rn/", file_get_contents("cluster.txt"));
$dt= json_encode($data, JSON_UNESCAPED_SLASHES);
$final_dt=stripslashes($dt);
$final_dt_arr=json_decode($final_dt,true);
//echo "<pre>"; print_r($final_dt_arr);
echo $final_dt;
4
Answers
I take a look on your script above, you already used
json_decode
, this is a good start.Can you please try the code below (most of my projects) use it:
file_get_contents()
function reads the JSON file into a stringjson_decode()
into a PHP objectforeach
loop then iterates over each element of the Employees array in the object, extracting the desired data.you can also pass the second argument as true to return an associative array instead.
@Lia, First you will pick only json data from your string, then you will convert to array. here goes your code.
If the file will always have exactly 3 lines of non-JSON content at the beginning and end, you can just remove that extraneous content from the data before trying to decode it, and then resume decoding and using the JSON data in the usual way.
For example:
Working demo: https://3v4l.org/Ar7ne
Well, as I saw all the comments about this crapy file content with text and JSON mixed together, I think the only thing you could do is to try to extract only the JSON part of the file which is between the opening and closing braces (
{
…}
).This could be done with the help of a regular expression. It’s clearly not very safe because the surrounding text could also have those chars. We’ll say it’s not the case…
The simple regular expression would be
/{.*}/s
But we can improve it a bit, as we know that it should contain
the
Employees
entry. This can help if the rest of the file alsohas some braces in the comments. The regex would become:
To test it with crapy content and have the explanation of the pattern: https://regex101.com/r/wPGwIk/1
The full PHP code:
Execution output:
You can run it here: https://onlinephp.io/c/5386c