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

2
  • how are you getting these lan and wan address, is it from a json request ?? Commented Oct 9, 2012 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. Commented Oct 9, 2012 at 20:10

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

1 Comment

This did the trick, just had to change $json to return my the variable instead of that text, but worked in the end. Thanks!
1

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

Comments

1

You could use something like json_decode()

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.