Solved

regex / cURL question

Posted on 2006-10-23
6
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

636 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