Jump to content

preg_match


Destramic
Go to solution Solved by Destramic,

Recommended Posts

im trying to make a script so i can get users details like longitude, latitude etc

 

but im having trouble matching the html by regular expression

 

here is the html below that im trying to extract the data from:

 

any help or possible any alternative, suggested ways would be greatful thank you

<td>Country:</td>
<td><img src="/images/dot.gif" class="flag-16 gb" align="absmiddle" width="16" height="16" title="United Kingdom"> United Kingdom (GB)</td>
			</tr>
	<tr>
		<td>City:</td>
		<td>Newport</td>
	</tr>
	<tr>
		<td>Region:</td>
		<td>Newport</td>
	</tr>
	<tr>
		<td>Latitude:</td>
		<td>51.5833</td>
	</tr>
	<tr>
		<td>Longitude:</td>
		<td>-2.9833</td>
	</tr>
	<tr>
		<td>Timezone:</td>
		<td>Europe/London</td>
<?php

$url = "http://smart-ip.net/geoip/2.101.108.124/";

$ch      = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);

if (preg_match('/<td>Country:</td><td>[img](?P<country>\w+)</td>/$', $rawdata, $match))
{
	$country = $match['country'];
}
if (preg_match('/<td>City:</td><td>(?P<city>\w+)</td>/$', $rawdata, $match))  
{
	$city = $match['city'];
}
if (preg_match('/<td>Region:</td><td>(?P<region>\w+)</td>/$', $rawdata, $match))
{
	$region = $match['region'];
}
if (preg_match('/<td>Latitude:</td><td>(?P<latitude>\d+)</td>/$', $rawdata, $match))
{
	$latitude = $match['latitude'];
}
if (preg_match('/<td>Longitude:</td><td>(?P<longitude>\d+)</td>/$', $rawdata, $match))
{
	$longitude = $match['longitude'];
}
if (preg_match('/<td>Timezone:</td><td>(?P<timezone>\w+)</td>/$', $rawdata, $match))
{
	$timezone = $match['timezone'];
}
?>
Link to comment
Share on other sites

  • Solution

thank you...ive found a easier way of getting most of the information i need now

protected function fetch_data()
	{
		$ip   = $this->get_ip();
		$url  = "http://www.geoplugin.net/json.gp?ip=" . $ip;
		$json = file_get_contents($url);
		$json = json_decode($json);
	
		$this->_ip                        = $ip;
		$this->_status                    = $json->{'geoplugin_status'};
		$this->_city                      = $json->{'geoplugin_city'};
		$this->_region                    = $json->{'geoplugin_region'};
		$this->_area_code                 = $json->{'geoplugin_areaCode'};
		$this->_dma                       = $json->{'geoplugin_dmaCode'};
		$this->_country_code              = $json->{'geoplugin_countryCode'};
		$this->_country_name              = $json->{'geoplugin_countryName'};
		$this->_continent_code            = $json->{'geoplugin_continentCode'};
		$this->_longitude                 = $json->{'geoplugin_longitude'};
		$this->_latitude                  = $json->{'geoplugin_latitude'};
		$this->_region_code               = $json->{'geoplugin_regionCode'};
		$this->_region_name               = $json->{'geoplugin_regionName'};
		$this->_currency['code']          = $json->{'geoplugin_currencyCode'};
		$this->_currency['symbol']        = $json->{'geoplugin_currencySymbol'};
		$this->_currency['symbol_UTF8']   = $json->{'geoplugin_currencySymbol_UTF8'};
		$this->_currency['exchange_rate'] = $json->{'geoplugin_currencyConverter'};
	}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.