开发者

How would I parse "var coords = {'x' : 982, 'y' : 1002 };" in php? [duplicate]

This question already has an answer here: How to extract and access data from JSON with PHP? (1 answer) Closed 11 months ago.
var coords = {'x' : 982, 'y' : 1002 };

The above code is returned开发者_如何学Python by the API when access via Curl.

I need to parse the x and y values into variables. The two values are also not always the same length. I'm not sure what the best way to do this is.

My idea was to use substr to cut off the front and back so it's 'x' : 982, 'y' : 1002, use explode to get a var with 'x' : 982 and another with 'y' : 1002, then use explode again to get 982 and 1002, and finally remove the spaces.

I'm unsure if this is the right path or not. Is this the right way to do it or would you do this another way?

Also, the API I am using is meant for Javascript but I am using PHP, they don't have a PHP API.

Edit:

I have:

<?php
$result = "var coords = {'x' : 982, 'y' : 1002 };";
$result = substr($result, 13);
$result = substr($result, 0,strlen ($result) - 1);
$json_obj = json_decode($result);
$x_coord = $json_obj->{'x'};
$Y_coord = $json_obj->{'y'};
echo 'x:' . $x_coord;
echo '<br>';
echo 'y:' . $y_coord;
?>

now but that doesn't seem to work.


It's simple, just use the json_decode function.


json_decode won't work because although the string is valid JavaScript, it is not valid JSON.

If the string format is exactly as you posted, I'd simply use preg_match_all:

preg_match_all('/([0-9]+)/', $input, $matches);
list($x, $y) = $matches[0];

The reason is simple: while it could be done without regex, less code equals less problems.


Use json_decode. This will produce either an object or an array, depending on the assoc parameter.


If you can be sure of the format of the response then you can use the code below:

<?php
$return = "var coords = {'x' : 982, 'y' : 1002 };";
$json = str_replace("'", '"', substr($return, strlen('var coords = '), -1));
$coords = json_decode($json);
var_dump($coords);

This relies on the response being just as you posted, it won't work if there are multiple lines or there are single quotes within strings etc.

Running json_decode on $return will not work because it is no JSON, it is a javascript (as you said this API is intented for Javascript).

So first we remove the variable assignment and get the result:

{'x' : 982, 'y' : 1002 }

I believe this is valid JSON depending on what you are using to decode it. In PHP this isn't valid, because the keys/names must be enclosed in double quotes not single quotes.

So with a str_replace on single to double quotes

{"x" : 982, "y" : 1002 }

No we have valid JSON for PHP. If you json_decode this you will get a stdClass object. And can access the values like so:

$coords = json_decode($json);
echo $coords->x;
echo $coords->y;


<?php
$json= '{"x" : 982, "y" : 1002 }';
$obj = json_decode($json);
print($obj->{'x'}); //this will give you 982
print($obj->{'y'}); //this will give you 1002
?>

json_decode()

UPDATED, the reason your code did not work is because you must use double quote to cover x and y, sorry i did not read your entire question throughly and basically provided you with merely the tool to parse JSON. Did not know you have a javascript statement. I apologize:

<?php
$result = "var coords = {'x' : 982, 'y' : 1002 };";
$result = substr($result, 13);
$result = substr($result, 0,strlen ($result) - 1);
$result = preg_replace("/'/", '"', $result);
$json_obj = json_decode($result);
$x_coord = $json_obj->{'x'};
$Y_coord = $json_obj->{'y'};
print('x coord: ' . $json_obj->{'x'}.PHP_EOL);
print('y coord: ' . $json_obj->{'y'});
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜