Solved

regex / cURL question

Posted on 2006-10-23
6
311 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

864 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now