Link to home
Start Free TrialLog in
Avatar of fixitben
fixitben

asked on

PHP JSON Clean up

HI

I have the text below returned from a json query, but it has /*JSON:Sessions*/ at the beginning and /* 0 SNTL_ADMIN_STATUS_OK */  at the end. I was able to do to a simple string replace for the first one since it is on one line. the second one is really 6 different lines. Is there an easy way to just strip everything between /* */ from the text being returned?  That way it will catch both even though the second one is over many lines. My goal is to have it in a JSON valid format.

/*JSON:sessions*/

{
"index":"1",
"sid":"0x00000003",
"acc":"00000003",
"vendorid":"47331",
"haspid":"2146096202",
"haspname":"2146096202",
"prid":"0",
"productname":"",
"prv":"Local",
"fid":"0",
"fn":"",
"cli":"172.16.12.19",
"usr":"Administrator",
"scrn":"console",
"mch":"USERID",
"pid":"9552",
"seats":"1",
"lt":"Wed Dec 7, 09:35:15",
"rt":"11:55:30",
"lm_version":"17000",
"lm_version_full":"17.0",
"lm_build":"17.0.1.48292",
"api_version":"7.03",
"ddis":"0"
},
{
"index":"2",
"sid":"0x00000004",
"acc":"00000004",
"vendorid":"47331",
"haspid":"2146096202",
"haspname":"2146096202",
"prid":"0",
"productname":"",
"prv":"Local",
"fid":"0",
"fn":"",
"cli":"172.16.12.19",
"usr":"Administrator",
"scrn":"console",
"mch":"USERID",
"pid":"9552",
"seats":"1",
"lt":"Wed Dec 7, 09:35:22",
"rt":"11:15:40",
"lm_version":"17000",
"lm_version_full":"17.0",
"lm_build":"17.0.1.48292",
"api_version":"7.03",
"ddis":"0"
},
{
"index":"3",
"sid":"0x00000009",
"acc":"00000009",
"vendorid":"47331",
"haspid":"2146096202",
"haspname":"2146096202",
"prid":"0",
"productname":"",
"prv":"Local",
"fid":"0",
"fn":"",
"cli":"172.16.12.19",
"usr":"Administrator",
"scrn":"console",
"mch":"USERID",
"pid":"9552",
"seats":"1",
"lt":"Wed Dec 7, 09:35:23",
"rt":"11:51:33",
"lm_version":"17000",
"lm_version_full":"17.0",
"lm_build":"17.0.1.48292",
"api_version":"7.03",
"ddis":"0"
},
{
"index":"4",
"sid":"0x0000000A",
"acc":"0000000A",
"vendorid":"47331",
"haspid":"2146096202",
"haspname":"2146096202",
"prid":"0",
"productname":"",
"prv":"Local",
"fid":"0",
"fn":"",
"cli":"172.16.12.19",
"usr":"Administrator",
"scrn":"console",
"mch":"USERID",
"pid":"9552",
"seats":"1",
"lt":"Wed Dec 7, 09:35:25",
"rt":"11:59:56",
"lm_version":"17000",
"lm_version_full":"17.0",
"lm_build":"17.0.1.48292",
"api_version":"7.03",
"ddis":"0"
},
{
"index":"5",
"sid":"0x0000000B",
"acc":"0000000B",
"vendorid":"47331",
"haspid":"2146096202",
"haspname":"2146096202",
"prid":"77",
"productname":"47331 Product 77",
"prv":"Local",
"fid":"28",
"fn":"",
"cli":"172.16.12.19",
"usr":"Administrator",
"scrn":"console",
"mch":"USERID",
"pid":"9552",
"seats":"1",
"lt":"Wed Dec 7, 09:35:25",
"rt":"11:15:35",
"lm_version":"17000",
"lm_version_full":"17.0",
"lm_build":"17.0.1.48292",
"api_version":"7.03",
"ddis":"0"
},
{
"index":"6",
"sid":"0x0000000C",
"acc":"0000000C",
"vendorid":"47331",
"haspid":"2146096202",
"haspname":"2146096202",
"prid":"71",
"productname":"47331 Product 71",
"prv":"Local",
"fid":"26",
"fn":"",
"cli":"172.16.12.19",
"usr":"Administrator",
"scrn":"console",
"mch":"USERID",
"pid":"9552",
"seats":"1",
"lt":"Wed Dec 7, 09:35:25",
"rt":"11:15:35",
"lm_version":"17000",
"lm_version_full":"17.0",
"lm_build":"17.0.1.48292",
"api_version":"7.03",
"ddis":"0"
},
{
"index":"7",
"sid":"0x0000000D",
"acc":"0000000D",
"vendorid":"47331",
"haspid":"2146096202",
"haspname":"2146096202",
"prid":"62",
"productname":"47331 Product 62",
"prv":"Local",
"fid":"20",
"fn":"",
"cli":"172.16.12.19",
"usr":"Administrator",
"scrn":"console",
"mch":"USERID",
"pid":"9552",
"seats":"1",
"lt":"Wed Dec 7, 09:35:25",
"rt":"11:15:35",
"lm_version":"17000",
"lm_version_full":"17.0",
"lm_build":"17.0.1.48292",
"api_version":"7.03",
"ddis":"0"
},
{
"index":"8",
"sid":"0x0000000E",
"acc":"0000000E",
"vendorid":"47331",
"haspid":"2146096202",
"haspname":"2146096202",
"prid":"46",
"productname":"47331 Product 46",
"prv":"Local",
"fid":"16",
"fn":"",
"cli":"172.16.12.19",
"usr":"Administrator",
"scrn":"console",
"mch":"USERID",
"pid":"9552",
"seats":"1",
"lt":"Wed Dec 7, 09:35:26",
"rt":"11:15:36",
"lm_version":"17000",
"lm_version_full":"17.0",
"lm_build":"17.0.1.48292",
"api_version":"7.03",
"ddis":"0"
},
{
"index":"9",
"sid":"0x00000010",
"acc":"00000010",
"vendorid":"47331",
"haspid":"2146096202",
"haspname":"2146096202",
"prid":"0",
"productname":"",
"prv":"Local",
"fid":"0",
"fn":"",
"cli":"172.16.12.19",
"usr":"Administrator",
"scrn":"console",
"mch":"USERID",
"pid":"9552",
"seats":"1",
"lt":"Wed Dec 7, 09:35:31",
"rt":"11:59:56",
"lm_version":"17000",
"lm_version_full":"17.0",
"lm_build":"17.0.1.48292",
"api_version":"7.03",
"ddis":"0"
},
{
"index":"10",
"sid":"0x00000011",
"acc":"00000011",
"vendorid":"47331",
"haspid":"2146096202",
"haspname":"2146096202",
"prid":"0",
"productname":"",
"prv":"Local",
"fid":"0",
"fn":"",
"cli":"192.168.12.21",
"usr":"sbouse",
"scrn":"console",
"mch":"SBOUSE-L",
"pid":"2192",
"seats":"1",
"lt":"Wed Dec 7, 10:16:48",
"rt":"11:57:02",
"lm_version":"17000",
"lm_version_full":"17.0",
"lm_build":"17.0.1.48292",
"api_version":"7.03",
"ddis":"0"
},
{
"fhaspid":"0",
"fprod":"0",
"ffea":"0",
"cnt":"10"
}
/*
 
  0
  SNTL_ADMIN_STATUS_OK
 
*/

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What is the API that you're getting this JSON from?  Is there any online documentation?

How are you capturing the JSON?  Are you looking at it in a browser?  If so, try using "view source" to see a better representation of the data.  Please post the "view source" if it's any different from what we see here, thanks.
Avatar of fixitben
fixitben

ASKER

Well it is kind of a hack and no actual API. We have a licensing software that has a webpage that displays license usage. I was able to find the url/json request that was being sent to display the information on the licensing page provided by them. Their licensing page is laid out horribly and will confuse our end users.  I already have  license viewer that I wrote that shows all the other manufactures license usage for various software. This software doesn't have an api or command line option to get this information. I would like to display this license managers usage on the same page to make it easy for the end users and have a similar format. Once I am able to clear the /* */ from the returned output it should be in a valid json format and will be workable with php.

Thankss
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much. Worked like a charm. Those regex expressions always confuse me. Especially when there are slashes and *'s.
PHP preg_quote() helps out with the special characters.  Here are the Regular Expressions, annotated with explanatory comments...
// REMOVE EVERYTHING BETWEEN /* and */
$rgx
= '#'                // REGEX DELIMITER
. preg_quote('/*')   // SLASH-ASTERISK
. '.*?'              // ANYTHING OR NOTHING
. preg_quote('*/')   // ASTERISK-SLASH
. '#'                // REGEX DELIMITER
. 's'                // TREAT AS SINGLE LINE
;
$jso = preg_replace($rgx, '???', $jso);


// MAKE ARRAY NOTATION SQUARE BRACKETS AROUND THE JAVASCRIPT OBJECTS
$rgx
= '#'                // REGEX DELIMITER
. '^'                // AT START OF STRING
. preg_quote('???')  // STRING LITERAL
. '#'                // REGEX DELIMITER
. 's'                // TREAT AS SINGLE LINE
;
$jso = preg_replace($rgx, '[', $jso);

$rgx
= '#'                // REGEX DELIMITER
. preg_quote('???')  // STRING LITERAL
. '$'                // AT END OF STRING
. '#'                // REGEX DELIMITER
. 's'                // TREAT AS SINGLE LINE
;
$jso = preg_replace($rgx, ']', $jso);

Open in new window