Solved

regex / cURL question

Posted on 2006-10-23
6
313 Views
Last Modified: 2008-02-20

im need to separate the following into separate variables:

Country: UNITED STATES (US)
City: Somewhere, AK
Latitude: 12.7696
Longitude: -123.4588


I've got the cURL placing it into a result variable, but i need to separate it into:

$Country
$City
$Latitude
$Longitude

anyone ??
0
Comment
Question by:n8dog
  • 3
6 Comments
 
LVL 11

Accepted Solution

by:
AnthonyP9618 earned 250 total points
ID: 17793033
What about explode()?

<?php
$Country = "Country: UNITED STATES (US)";
$Country = explode(":", $Country);
echo $Country[0]; // This will echo "Country"
echo $Country[1]; // This will echo "UNITED STATES (US)"
?>

Copy the code for the rest of your values and you are set.
0
 

Author Comment

by:n8dog
ID: 17793065
ok..

keep in mind the result from the query is this:

UNITED STATES (US) City: Scottsdale, AZ Latitude: 33.686 Longitude: -111.87

and all portions are variable depending on the IP going in...

0
 

Author Comment

by:n8dog
ID: 17793068
country isnt always UNITED STATES (US), and so on...

0
 

Author Comment

by:n8dog
ID: 17793083
your explode code results in:

UNITED STATES (US) City: Scottsdale, AZ Latitude: 33.686 Longitude: -111.87 -> $result


$Country = $result;
$Country = explode(":", $Country);
echo $Country[1]; // This will echo "UNITED STATES (US)"


UNITED STATES (US) City

0
 
LVL 9

Assisted Solution

by:tolgaong
tolgaong earned 250 total points
ID: 17794097
Both works with carriage return or single line

<?php
$data="Country: UNITED STATES (US)
City: Somewhere, AK
Latitude: 12.7696
Longitude: -123.4588
";

//$data="Country: UNITED STATES (US) City: Somewhere, AK  Latitude: 12.7696 Longitude: -123.4588";

if (preg_match('/Country:(?P<country>.*)(\\r\\n*)?(?=City)City:(?P<city>.*)(\\r\\n*)?(?=Latitude)Latitude:(?P<latitude>.*)(\\r\\n*)?(?=Longitude)Longitude:(?P<longitude>.*)/i', $data, $regs)) {
      $country = trim($regs['country']);
      $city=trim($regs['city']);
      $latitude=trim($regs['latitude']);
      $longitude=trim($regs['longitude']);
      }
echo "$country $city $latitude $longitude";
?>
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question