0

I am getting data from an API and the resulting string is

[RESPONSE]
PROPERTY[STATUS][0]=ACTIVE
PROPERTY[REGISTRATIONEXPIRATIONDATE][0]=2012-04-04 19:48:48
DESCRIPTION=Command completed successfully
QUEUETIME=0
CODE=200
RUNTIME=0.352

QUEUETIME=0
RUNTIME=0.8

EOF

I am trying to convert this into an array like

Array(
['PROPERTY[STATUS][0]'] => ACTIVE,
['CODE'] => 200,
...
);

So I am trying to explode it using the resulting file_get_content function with an explode like

$output = explode('=',file_get_contents($url));

But the problem is the returning values are not always returned in the same order, so I need to have it like $array['CODE'] = 200, and $array['RUNTIME'] = 0.352 however there does not seem to be any kind of new line characters? I tried \r\n, \n, <br>, \r\n\r\n in the explode function to no avail. But there is new lines in both notepad and the browser.

So my question is there some way to determine if a string is on a new line or determine what the character forcing the new line is? If not is there some other way I could read this into an array?

0

5 Answers 5

1

To find out what the breaking character is, you could do this (if $data contatins the string example you've posted):

echo ord($data[strlen('[RESPONSE]')]) . PHP_EOL;
echo ord($data[strlen('[RESPONSE]')+1]);          // if there's a second char

Then take a look in the ASCII table to see what it is.

EDIT: Then you could explode the data using that newly found character:

explode(ord($ascii_value), $data);

Btw, does file() return a correct array?

Sign up to request clarification or add additional context in comments.

3 Comments

@Steven, when you have it's ASCII value, you can explode like this: explode(ord($ascii_value), $data);.
+1 Nice approach. But he's not telling us what character it was(?). I demand to be told!!! :}
LF, that's what I suggested in my answer, who the hell down-voted it?
1

Explode on "\n" with double quotes so PHP understands this is a line feed and not a backslashed n ;-) then explode each item on =

2 Comments

"however there does not seem to be any kind of new line characters? I tried \r\n, \n, <br>, \r\n\r\n in the explode function to no avail. But there is new lines in both notepad and the browser." I tried that and it doesn't seem to be exploding. See lii.in/drop_catch.php for example
Did you explode on '\n' or "\n"? The first one will fail
1

Why not just use parse_ini_file() or parse_ini_string()?

It should do everything you need (build an array) in one easy step.

2 Comments

@mario, interesting that PHP only supports one set of brackets. That's a silly restriction that I was unaware of.
I'm lookin at this from the upside: At least it supports one level of array syntax. Might not suffice in this case, but nice function to keep in mind.
0

Try

preg_split("/$/m", $str)

or

preg_split("/$\n?/m", $str)

for the split

Comments

0

The lazy solution would be:

$response = strtr($response, "\r", "\n");
preg_match_all('#^(.+)=(.+)\s*$#m', $response, $parts);
$parts = array_combine($parts[1], $parts[2]);

Gives you:

Array (
 [PROPERTY[STATUS][0]] => ACTIVE
 [PROPERTY[REGISTRATIONEXPIRATIONDATE][0]] => 2012-04-04 19:48:48
 [DESCRIPTION] => Command completed successfully
 [QUEUETIME] => 0
 [CODE] => 200
 [RUNTIME] => 0.8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.