I can not understand how to parse a string:

useriata({"iata":"MOW","name":"Москва"}) 

This is not an array, and not json.

  • Is this a string, that is, $data = 'useriata({"iata":"MOW","name":"Москва"})' ? - PinkTux
  • @PinkTux send a request to the API, and it returns the useriata({"iata":"MOW","name":"Москва"}) string useriata({"iata":"MOW","name":"Москва"}) to me - JamesJGoodwin
  • Ah, so are these travelpayouts? First, they know how to give JSON, you need to delve into the developer documentation. Secondly, you can simply cut the useriata(...) and parse as usual JSON. - PinkTux

2 answers 2

This data transfer format is called JSONP .

And parsing it is very simple: you bite the wrapping function and parse JSON using standard tools.

For example:

 $jsonp_data = 'useriata({"iata":"MOW","name":"Москва"})'; $json = preg_replace('/^[^\(]+\((.*)\)$/', '$1', $jsonp_data); var_dump(json_decode($json)); 

    Most likely - this is JSONP. In this case, you can simply throw out ^useriata(\{ and \)$ (not strong in the regulars syntax, but the point is to remove the beginning and end of the line)