Need Help with PHP REGEX

Hello, I have this script:

<?php

function getLocation() {
$ip = $_SERVER['REMOTE_ADDR'];
$geo = file_get_contents("http://api.hostip.info/get_html.php?ip=$ip");
$results = urlencode($geo);
echo $results;
}

getLocation();

?>

Which returns:

Country%3A+CANADA+%28CA%29%0ACity%3A+ST.+JOHN%27S%2C+NL%0AIP%3A+142.162.13.207%0A

I would like for $results to contain only "ST.+JOHN%27S%2C+NL"; in other words the city name and the province/state for the visiting user.

What I'm aiming to do is make the regex change after this line:

$geo = file_get_contents("http://api.hostip.info/get_html.php?ip=$ip");

and before this line:

$results = urlencode($geo);

This way the modified data (ie. "City%2C+State") gets passed off into $results.
I need $results encoded because this script is going to be part of another script where $results get passed off as part of a URL parameter.

Regex seems to work differently with PHP, and I don't have much experience with regex or arrays; I'm just learning as I go :slight_smile:
Any help would be greatly appreciated. Thanks :slight_smile:

Before using preg_match() on your $results, one approach would be split your $results into an array using the "+" delimiter that is in the $results string first.

For this you would use explode().

So first do something like this:

$my_parts = explode ("+", $results);

Resulting in:

$my_part[0]= Country%3A
$my_part[1]= CANADA
$my_part[2]= %28CA%29%0ACity%3A
$my_part[3]= ST.
$my_part[4]= JOHN%27S%2C  
$my_part[5]= NL%0AIP%3A
$my_part[6]= 142.162.13.207%0A

Or you could explode on ""%3A+" like this:

$my_parts = explode ("%3A+", $results);

Resulting in:

$my_part[0]= Country
$my_part[1]= CANADA+%28CA%29%0ACity
$my_part[2]= ST.+JOHN%27S%2C+NL%0AIP
$my_part[3]= 142.162.13.207%0A  

If you explode() twice, I think you can get the substrings you are looking for without preg_match(), then combine them, of course. Or you can simply preg_replace() the offending "IP" with "" at the end of $my_part[2] ... up to you.

It might work for you to simply explode() on "%3A" first, it's up to you (not sure of your final output.. but it seems "%3A+" is best). Your string:

$my_part[0]= Country
$my_part[1]= +CANADA+%28CA%29%0ACity
$my_part[2]= +ST.+JOHN%27S%2C+NL%0AIP
$my_part[3]= +142.162.13.207%0A  

Hopefully, I did not make too many "cut and paste" errors in my examples!

[RESOLVED]
file_get_contents() and preg_match() did the trick :slight_smile:

Thanks for all your help!

Post your code so we can see it :wink:

Rough Copy:

<?php

function getWeather($ip=false) {
    
   if (!empty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    $url = "http://api.hostip.info/get_html.php?ip=$ip";
    $geo = file_get_contents($url);
    if( preg_match('/City: (.*)\nIP:/', $geo, $matches) ){
        $results = $matches[1];
    }else{
        $results = 'getLocation failure';

    }

$location = urlencode($results);
$getAddress = "http://www.google.com/ig/api?weather=$location";
$xml_str = file_get_contents($getAddress,0);
$xml = new SimplexmlElement($xml_str);
$count = 0;
echo '<div id="weather">';
foreach($xml->weather as $item) {

foreach($item->forecast_information as $new) {
            echo $new->city['data'];
            }

        foreach($item->current_conditions as $new) {

            echo '<div class="weatherIcon">';
            echo '<img src="http://www.google.com/' .$new->icon['data'] . '"/><br/>';
        echo $new->condition['data'];
            echo $new->temp_f['data'];
            echo $new->temp_c['data'];
            echo $new->humidity['data'];
            echo $new->wind_condition['data'];
            echo '</div>';
            }

        foreach($item->forecast_conditions as $new) {

            echo '<div class="weatherIcon">';
            echo '<img src="http://www.google.com/' .$new->icon['data'] . '"/><br/>';
            echo $new->day_of_week['data'];
            echo $new->condition['data'];
            echo $new->low['data'];
            echo $new->high['data'];
            echo '</div>';
            }

    }

echo '</div>';
}

getWeather();

?>

Thanks!

Looks good. Thanks for posting.

---------- Post updated at 18:51 ---------- Previous update was at 18:48 ----------

OBTW, your code will run faster if you download and install GeoIP vs call across the net to HostIP each time.

Can I install GeoIP in RED HAT Linux? My server runs RED HAT.

Yes, of course. If you run Apache2 web server you can also simply install mod_geoip.

---------- Post updated at 19:13 ---------- Previous update was at 19:09 ----------

Here is a quick tutorial on this:

Installing mod_geoip on CentOS 5.x or RHEL Tales from the Script

Thanks!