Solved

regex / cURL question

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
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.

758 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

18 Experts available now in Live!

Get 1:1 Help Now