-1

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
Dan
  • 19
  • 3
  • how are you getting these lan and wan address, is it from a json request ?? – Aman Virk Oct 09 '12 at 19:43
  • I'm actually getting it from someone else's server via @file_get_contents, so I'm unsure how the actual source code looks. – Dan Oct 09 '12 at 20:10

5 Answers5

2
$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] .

Spaceploit
  • 327
  • 3
  • 12
  • This did the trick, just had to change $json to return my the variable instead of that text, but worked in the end. Thanks! – Dan Oct 09 '12 at 20:13
1

It looks like your "printed answer" is JSON. In which case parse the json then extract the needed values.

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
1

You could use something like json_decode()

intelis
  • 7,829
  • 14
  • 58
  • 102
1

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 :)

alan978
  • 535
  • 3
  • 6
1

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

Jesse Q
  • 1,451
  • 2
  • 13
  • 18