Related
I have a JSON file that I want to use PHP to replace the "Systems_x0020_Changed_IDs" value from a string to an array. "39122" becomes [39122] and "39223, 39244, 39395" becomes [39223, 39244, 39395]. I am using http://www.regexpal.com/ to test my expression. The expression is:
"([(0-9)+((, *))]+)+"
This is producing unexpected results in PHP. In my JSON file:
[{
"ID": 1050436,
"Title": "THE SKY IS FALLING!!!!",
"Application_x0020_ID": 242,
"Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont",
"Systems_x0020_Changed_IDs": "39122",
"Status": "New",
"Modified": "2015-10-28T16:14:45.573-04:00",
"Age": 40,
"Description_x0020__x0028_Public_x0029_": "I'm chicken little and the SKY IS FALLING!",
"Impact_x0020__x0028_Public_x0029_": "The world is going to end!",
"Start_x0020_Time": "2015-10-28T00:00:00-04:00",
"End_x0020_Time": "2015-10-30T00:00:00-04:00",
"Hours": 12
}, {
"ID": 1050740,
"Title": "This is a Title",
"Application_x0020_ID": 242,
"Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"",
"Systems_x0020_Changed_IDs": "39223, 39244, 39395",
"Status": "New",
"Modified": "2015-11-05T17:31:13.15-05:00",
"Age": 32,
"Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients",
"Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.",
"Start_x0020_Time": "2015-11-27T08:38:00-05:00",
"End_x0020_Time": "2015-11-30T00:00:00-05:00",
"Hours": 1
}]
Several commas at the end of lines are being replaced with brackets[] so that the output looks like:
[{
"ID": 1050436,
"Title": "THE SKY IS FALLING!!!![,]Application_x0020_ID": 242,
"Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont[,]Systems_x0020_Changed_IDs": 39122,
"Status": "New[,]Modified": "2015-10-28T16:14:45.573-04:00[,]Age": 40,
"Description_x0020__x0028_Public_x0029_": "I'm chicken little and the SKY IS FALLING![,]Impact_x0020__x0028_Public_x0029_": "The world is going to end![,]Start_x0020_Time": "2015-10-28T00:00:00-04:00[,]End_x0020_Time": "2015-10-30T00:00:00-04:00[,]Hours": 12
}, {
"ID": 1050740,
"Title": "This is a Title[,]Application_x0020_ID": 242,
"Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"[,]Systems_x0020_Changed_IDs": [39223, 39244, 39395],
"Status": "New[,]Modified": "2015-11-05T17:31:13.15-05:00[,]Age": 32,
"Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients[,]Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.[,]Start_x0020_Time": "2015-11-27T08:38:00-05:00[,]End_x0020_Time": "2015-11-30T00:00:00-05:00[,]Hours": 1
}]
My question is, how can I modify the expression so that PHP will behave like regexpal.com and only get the numbers within quotes and ignore the rest?
Your regex is rather strange, you appear to be trying to put a pattern expression inside a character class [...], which is probably not doing what you'd expect. Furthermore, your regex would match values inside other key/value pairs. Try this instead, which will only match values for the key "Systems_x0020_Changed_IDs":
"Systems_x0020_Changed_IDs":\s+"([^"]*)"
What about just parsing it as the JSON that it is?
$jsons = array('{
"ID": 1050436,
"Title": "THE SKY IS FALLING!!!!",
"Application_x0020_ID": 242,
"Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont",
"Systems_x0020_Changed_IDs": "39122",
"Status": "New",
"Modified": "2015-10-28T16:14:45.573-04:00",
"Age": 40,
"Description_x0020__x0028_Public_x0029_": "I\'m chicken little and the SKY IS FALLING!",
"Impact_x0020__x0028_Public_x0029_": "The world is going to end!",
"Start_x0020_Time": "2015-10-28T00:00:00-04:00",
"End_x0020_Time": "2015-10-30T00:00:00-04:00",
"Hours": 12
}', '{
"ID": 1050740,
"Title": "This is a Title",
"Application_x0020_ID": 242,
"Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"",
"Systems_x0020_Changed_IDs": "39223, 39244, 39395",
"Status": "New",
"Modified": "2015-11-05T17:31:13.15-05:00",
"Age": 32,
"Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients",
"Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.",
"Start_x0020_Time": "2015-11-27T08:38:00-05:00",
"End_x0020_Time": "2015-11-30T00:00:00-05:00",
"Hours": 1
}');
foreach($jsons as $json){
$json_array = json_decode($json, true);
echo $json_array['Systems_x0020_Changed_IDs'] . "\n";
}
Demo: https://eval.in/481865
If you needed a regex you could do something like:
"Systems_x0020_Changed_IDs":\h*"(([\d+],?\h*)*)"
Demo: https://regex101.com/r/yZ6eM3/1
PHP Usage:
$string = '{
"ID": 1050436,
"Title": "THE SKY IS FALLING!!!!",
"Application_x0020_ID": 242,
"Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont",
"Systems_x0020_Changed_IDs": "39122",
"Status": "New",
"Modified": "2015-10-28T16:14:45.573-04:00",
"Age": 40,
"Description_x0020__x0028_Public_x0029_": "I\'m chicken little and the SKY IS FALLING!",
"Impact_x0020__x0028_Public_x0029_": "The world is going to end!",
"Start_x0020_Time": "2015-10-28T00:00:00-04:00",
"End_x0020_Time": "2015-10-30T00:00:00-04:00",
"Hours": 12
}, {
"ID": 1050740,
"Title": "This is a Title",
"Application_x0020_ID": 242,
"Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"",
"Systems_x0020_Changed_IDs": "39223, 39244, 39395",
"Status": "New",
"Modified": "2015-11-05T17:31:13.15-05:00",
"Age": 32,
"Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients",
"Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.",
"Start_x0020_Time": "2015-11-27T08:38:00-05:00",
"End_x0020_Time": "2015-11-30T00:00:00-05:00",
"Hours": 1
}';
$regex = '/"Systems_x0020_Changed_IDs":\h*"((?:[\d+],?\h*)*)"/';
preg_match_all($regex, $string, $matches);
print_r($matches[1]);
Output:
Array
(
[0] => 39122
[1] => 39223, 39244, 39395
)
Demo #2: https://eval.in/481871
The answer I was looking for is:
$str = preg_replace('/"((\d+[, ]*)+)"/', "[$1]", $str);
I needed the JSON file as is except for number values as strings. My regex worked after I played with it a little more.
I'm trying to parse this echonest request:
{"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "start": 0, "total": 1, "biographies": [{"text": "Pianist, composer and leader (8 September 1893 - 25 January 1947) Complete name: Adolfo Carabelli", "site": "last.fm", "url": "http://www.last.fm/music/Adolfo+Carabelli/+wiki", "license": {"type": "cc-by-sa", "url": "http://creativecommons.org/licenses/by-sa/3.0/", "version": "3.0"}}]}}
My code:
$biographie = $jbios->response->biographies[1]->text;
echo "<b> Biographie: </b>". $biographie."<br>";
What's wrong ?
Thanks in advance,
Have you did json_decode?
Please try:
$array = json_decode($json,true)
This is a snippet of code inside my JSON parser function, which is working fine with about a dozen or so various JSON files:
The prints are there for current debugging purposes
$this->response = stripslashes($this->response);
print_r($this->response);
$this->response = json_decode($this->response);
print_r($this->response);
The first print gives me the JSON string, and the second print gives me null. Using json_last_error I managed to find out that PHP was refusing to parse the JSON as it was in an invalid syntax.
This is a snipped of the JSON I'm parsing (full thing here), it validates with every validator I can find on Google:
{
"success": 1,
"stores": [
{
"name": "Winton",
"address": "370-374 , Wimborne Road, Bournemouth, Dorset BH92HE",
"telephone": "",
"email": "info#99pstoresltd.com",
"website": "",
"description": "Mon - 09.00-18.00 Tue - 09.00-18.00 Wed - 09.00-18.00 Thu - 09.00-18.00 Fri - 09.00-18.00 Sat - 09.00-18.00 Sun - 10.00-16.00",
"lat": "50.7413",
"lng": "-1.87926",
"titlewebsite": "Website",
"titleemail": "Email",
"titletel": "Telephone",
"titlecontactstore": "Contact this store",
"titlekm": "km",
"titlemiles": "miles",
"cat_name": "",
"cat_img": "",
"img": ""
},
{
"name": "Boscombe",
"address": "The Sovereign Centre, Boscombe, Bournemouth, Dorset BH14SX",
"telephone": "",
"email": "info#99pstoresltd.com",
"website": "",
"description": "Mon - 08.00-18.00 Tue - 08.00-18.00 Wed - 08.00-18.00 Thu - 08.00-18.00 Fri - 08.00-18.00 Sat - 08.00-18.00 Sun - 10.00-16.00",
"lat": "50.7272",
"lng": "-1.83952",
"titlewebsite": "Website",
"titleemail": "Email",
"titletel": "Telephone",
"titlecontactstore": "Contact this store",
"titlekm": "km",
"titlemiles": "miles",
"cat_name": "",
"cat_img": "",
"img": ""
}]
}
I have no idea why this isn't parsing as the JSON looks fine to me! Been staring at this for quite a while now so would be more than happy to here some thoughts on this.
EDIT:
Copy and pasting the data from the first print_r and pushing it through the json_decode works fine. I'm assuming this means there is an issue with where the JSON is originating from which could be messing up encoding or something?
Try this:
json_decode($this->response, true);
From the documentation about the second parameter:
When TRUE, returned objects will be converted into associative arrays.
Source: http://php.net/manual/en/function.json-decode.php
I have php code that returns this from a database,
[
{
"Time": "2012-11-27 16:10:35",
"NumPlayers": "1"
},
{
"Time": "2012-11-27 16:24:55",
"NumPlayers": "1"
},
{
"Time": "2012-11-27 16:25:37",
"NumPlayers": "2"
},
{
"Time": "2012-11-27 16:29:33",
"NumPlayers": "2"
The times are MySQL timestamps. I need to get it into this format to use with Highcharts javascript charting.
data: [
[Date.UTC(1970, 9, 9), 0 ],
[Date.UTC(1970, 9, 14), 0.15],
[Date.UTC(1970, 10, 28), 0.35],
[Date.UTC(1970, 11, 12), 0.46],
I'm having trouble figuring out how to loop through the MySQL results and convert the timestamps to javascript Date.UTC objects. I need to place the NumPlayers value after the Date.UTC objects, and output it in the format below. I'm a PHP and javascript noob :\
It should look something like this:
data: [
[Date.UTC(2012, 11, 27, 16, 10, 35), 1],
[Date.UTC(2012, 11, 27, 16, 24, 55), 1],
[Date.UTC(2012, 11, 27, 16, 25, 37), 2],
[Date.UTC(2012, 11, 27, 16, 29, 33), 2],
You should realize that Date.UTC(2012, 11, 27, 16, 10, 35) simply returns the number of milliseconds since the epoch (1356624635000). Therefore, you can just convert your object into UNIX timestamps (times a 1000 since JS works with millisecond timestamps, but PHP works with seconds).
Sample Code
$data = '[{"Time": "2012-11-27 16:10:35", "NumPlayers": "1"}, {"Time": "2012-11-27 16:24:55", "NumPlayers": "1"}]';
// Make sure date is parsed as UTC
date_default_timezone_set("UTC");
// Convert items into the desired format
$mapper = function($item) {
return array(strtotime($item->Time)*1000, $item->NumPlayers);
}
echo json_encode(array_map($mapper, json_decode($data)));
Output
[[1354032635000,"1"],[1354033495000,"1"]]
You seem to be getting straight JSON from your database, which you can always convert into an array:
$data = '[{"Time": "2012-11-27 16:10:35", "NumPlayers": "1"}, {"Time": "2012-11-27 16:24:55", "NumPlayers": "1"}]';
$arrayData = json_decode($data, true);
After which you can simply iterate through the array and print out the contents of the array in the JS format you need. Something like that:
echo 'data: [' . PHP_EOL;
foreach ($arrayData as $item) {
echo '[Date.UTC(';
$tmp = preg_split( '/(-| |\:)/', $item['Time'] );
echo implode(', ', $tmp);
echo '), ' . $item['NumPlayers'] . '],';
echo PHP_EOL;
}
echo PHP_EOL . '];';
You can split the time string into sections using /[^\d]+/g:
var finalData = [], numbers=/[^\d]+/g, item, dateParts, newDate;
for (var i=0, l=data.length; i<l; i++) {
item = data[i];
dateParts = item.Time.split(numbers);
newDate = Date.UTC.apply(Date, dateParts);
finalData.push([newDate, item.NumPlayers]);
}
See also: MDN's documentation on JavaScript regular expressions (/[^\d]/g is a regex).
The best way to provide data to Highcharts is getting data formated from database, so that you don't have to do it on client side.
To do it you just have to change your query to something like the following.
Backend
$query = "SELECT UNIX_TIMESTAMP(date) * 1000 AS 'date', value FROM ...";
Then to send the result to frontend:
echo json_encode($result);
Frontend
To get the result on frontend, this case using jQuery and assuming that url is your url:
$.getJSON(url, function(json) {
// decode json
var data = $.parseJSON(json);
// Then you just have to pass it to your series
});
Or better, store date values in UTC.
var php_data = [
{
"Time": "2012-11-27 16:10:35",
"NumPlayers": "1"
},
{
"Time": "2012-11-27 16:24:55",
"NumPlayers": "1"
}
];
var length = php_data.length;
hc_data = [];
for(var i = 0; i< length; i++){
php_date = new Date(php_data[i]["Time"]);
hc_data.push(
[
Date.UTC(
php_date.getFullYear(),
php_date.getMonth() + 1,
php_date.getDate()
),
php_data[i]["NumPlayers"]
]
);
}
// hc_data is your Array
I have invalid external json data, without double quotes around names.
Example:
{
data: [
{
idx: 0,
id: "0",
url: "http://247wallst.com/",
a: [
{
t: "Title",
u: "http://247wallst.com/2012/07/30/",
sp: "About"
}
],
doc_id: "9386093612452939480"
},
{
idx: 1,
id: "-1"
}
],
results_per_page: 10,
total_number_of_news: 76,
news_per_month: [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
result_start_num: 2,
result_end_num: 2,
result_total_articles: 76
}
As you see a lot of names like data,idx,id,url and others are not double quoted, so this makes this json invalid.
How can I make this external json valid? I already tried str_replace, replacing '{' to '{"' and ':' to '":' adding double quotes around unquoted names, but this messes up some already double quoted variables.
How can I make this json valid so I can read this data with PHP json_decode? I'm not very familiar with preg_replace..
Valid json will look like:
{
"data": [
{
"idx": 0,
"id": "0",
"url": "http://247wallst.com/",
"a": [
{
"t": "Title",
"u": "http://247wallst.com/2012/07/30/",
"sp": "About"
}
],
"doc_id": "9386093612452939480"
},
{
"idx": 1,
"id": "-1"
}
],
"results_per_page": 10,
"total_number_of_news": 76,
"news_per_month": [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
"result_start_num": 2,
"result_end_num": 2,
"result_total_articles": 76
}
Please suggest me some php preg_replace function.
Data source:
http://www.google.com/finance/company_news?q=aapl&output=json&start=1&num=1
With preg_replace you can do:
json_decode(preg_replace('#(?<pre>\{|\[|,)\s*(?<key>(?:\w|_)+)\s*:#im', '$1"$2":', $in));
Since the above example won't work with real data (the battle plans seldom survive first contact with the enemy) heres my second take:
$infile = 'http://www.google.com/finance/company_news?q=aapl&output=json&start=1&num=1';
// first, get rid of the \x26 and other encoded bytes.
$in = preg_replace_callback('/\\\x([0-9A-F]{2})/i',
function($match){
return chr(intval($match[1], 16));
}, file_get_contents($infile));
$out = $in;
// find key candidates
preg_match_all('#(?<=\{|\[|,)\s*(?<key>(?:\w|_)+?)\s*:#im', $in, $m, PREG_OFFSET_CAPTURE);
$replaces_so_far = 0;
// check each candidate if its in a quoted string or not
foreach ($m['key'] as $match) {
$position = $match[1] + ($replaces_so_far * 2); // every time you expand one key, offsets need to be shifted with 2 (for the two " chars)
$key = $match[0];
$quotes_before = preg_match_all('/(?<!\\\)"/', substr($out, 0, $position), $m2);
if ($quotes_before % 2) { // not even number of not-escaped quotes, we are in quotes, ignore candidate
continue;
}
$out = substr_replace($out, '"'.$key.'"', $position, strlen($key));
++$replaces_so_far;
}
var_export(json_decode($out, true));
But since google offers this data in RSS feed, i would recommend you to use that one if it works for your usecase, this is just for fun (-:
The JSON feeds from Google always seem to be plagued with problems- formatted incorrectly in some way shape or form. If you switch the feed to RSS you can easily convert it to an array or JSON from the array.
<?php
$contents = file_get_contents('http://www.google.com/finance/company_news?q=aapl&output=rss&start=1&num=1');
// Convert the RSS to an array (probably just use this)
$arr = simplexml_load_string($contents);
// Or if you specifically want JSON
$json = json_encode($arr);
// And back to an array
print_r(json_decode($json));