Avatar of Teera Char
Teera Char
Flag 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

PHP

Avatar of undefined
Last Comment
slightwv (䄆 Netminder)

8/22/2022 - Mon
gr8gonzo

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
slightwv (䄆 Netminder)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
David Favor

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.
Scott Fell

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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
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.