Search code examples
phpsplitexplode

PHP Split string between several characters


I have a script which records your WAN and LAN IP, but it also has a bunch of unnecessary characters around the printed answer. How can I split the IPs and get rid of these characters?

Printed Answer: {"ip":[["WANIP","LANIP"]]} What I want is 2 different variables, 1 to print wan and 1 to print lan.

I've tried with str_split and explode, maybe I didn't do it right or I can't do it with these, so any answers would help.


Solution

  • $json = '{"ip":[["WANIP","LANIP"]]}';
    $decoded = json_decode($json, true); // true means it will format it into an assoc array
    

    Then you'll be able to access your wanted strings by simply using $decoded['ip'][0][0] and $decoded['ip'][0][1] .