Link to home
Start Free TrialLog in
Avatar of Teera Char
Teera CharFlag for Thailand

asked on

php cut unwanted from string

$respon  is string

{"token_type":"Bearer","client_id":"GQQZFVpjICA9clRqAPertreyC5XLX379","access_token":"PJVS9JiOFWCZ8F4dBCUEa7GdDnCO","scope":"","expires_in":"1799","status":"approved"}


How can I cut value remaining only  'PJVS9JiOFWCZ8F4dBCUEa7GdDnCO' it will follow "access_token" :       in php

Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

You use json_decode() to turn that string into an object or array, then you can access the value. Example:

$str = '{"token_type"...etc...}';
$token = json_decode($str, true);
echo $token["access_token"]; // OUTPUT: PJVS9...etc...

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

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
While both comments are correct, I generally take @gr8gonzo's approach... so... when I come back to my code years later...

I can glance at my code + determine what I had in mind.
I agree, slgihtwv's is a slick option. However, if you need any other data, I just find it easier to put the entire result in an array, then you can access any part of that array throughout your code and use any of multiple array functions https://www.php.net/manual/en/ref.array.php
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

I too agree:
If you need to pull several key/values from the JSON, then load it up and pull all you need.

If you only need one, don't waste the overhead of storing the array in memory in a variable just in case you might want others.