Destramic Posted October 26, 2014 Share Posted October 26, 2014 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']; } ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted October 26, 2014 Share Posted October 26, 2014 (edited) I see a number of issues with your regex, for example, one problem I see is that your patterns aren't accounting for newlines and whitespace between html tags. But rather than try to make your regex work, you instead use a DOM parser for this. Edited October 26, 2014 by .josh Quote Link to comment Share on other sites More sharing options...
Solution Destramic Posted October 26, 2014 Author Solution Share Posted October 26, 2014 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'}; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.