-1

I get data when any user request to serach url in browser and i got her ip using that ip i convert that into string using API Like this.

$ip=1;US;USA;United States

here i need only iso3 code of that country how seprate that variable to get output like this.

$iso3=USA;
Sfili_81
  • 2,377
  • 8
  • 27
  • 36
Pratik Patel
  • 221
  • 1
  • 10
  • There should be a string split function. I think in PHP it's called explode – Brian Dec 07 '18 at 13:01
  • You may find a cool solution here - https://stackoverflow.com/questions/14133780/explode-a-string-to-associative-array-without-using-loops – Tushar Walzade Dec 07 '18 at 14:22

2 Answers2

0
$ipArr = explode(';',$ip);

if (is_array($ipArr)) {
    $iso3=$ipArr[2];
}
else {
    $iso3='same default value or error msg';
}
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
  • Your if statement is obsolete, you can simplify it as: `$iso3= $ipArr[2] ?? "default value or error msg"` see: http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op – Sven Hakvoort Dec 07 '18 at 13:14
  • Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Kurt Van den Branden Dec 07 '18 at 13:46
0

First you'll want to use explode with the ';' as the delimiter. This is going to cut up the string at each ';' that it contains.

Example:

$ip = "1;US;USA;United States";

$ipValues = explode(';', $ip);

The result is going to be:

  [
      0 => '1',
      1 => 'US',
      2 => 'USA',
      3 => 'United States',
  ]

Then you simply assign the ISO code to your variable like so:

$iso3 = $ipValues[2];
lucid
  • 422
  • 3
  • 15