'PHP Split string between several characters [duplicate]
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 1:[1]
$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] .
Solution 2:[2]
It looks like your "printed answer" is JSON. In which case parse the json then extract the needed values.
Solution 3:[3]
You could use something like json_decode()
Solution 4:[4]
try this code:
$data = "{\"ip\":[[\"WANIP\",\"LANIP\"]]}";
$jdecode = json_decode($data,true);
echo $jdecode['ip'][0][0];
echo $jdecode['ip'][0][1];
Hope this helps :)
Solution 5:[5]
I would first strip out some of the unnecessary clutter, then explode:
$response = '{"ip":[["WANIP","LANIP"]]}'; //or however you load your variable
$arryRemove = array('"', 'ip:[[', ']]}'); //specify an array of things to remove
$response = str_replace($arryRemove, "", $response);
$arryIPs = explode(",", $response);
$arryIPs[0] will contain WANIP,
$arryIPs[1] will contain LANIP
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Spaceploit |
| Solution 2 | Jon Taylor |
| Solution 3 | intelis |
| Solution 4 | alan978 |
| Solution 5 | Jesse Q |
