Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

regex / cURL question

Posted on 2006-10-23
6
Medium Priority
?
319 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 750 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 750 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
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.
Suggested Courses

926 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