Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 877215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:34:10+00:00 2026-05-15T11:34:10+00:00

How would I use Regex to get the information on a IP to Location

  • 0

How would I use Regex to get the information on a IP to Location API

This is the API

http://ipinfodb.com/ip_query.php?ip=74.125.45.100

I would need to get the Country Name, Region/State, and City.

I tried this:

$ip = $_SERVER["REMOTE_ADDR"];
$contents = @file_get_contents('http://ipinfodb.com/ip_query.php?ip=' . $ip . '');
$pattern = "/<CountryName>(.*)<CountryName>/";
preg_match($pattern, $contents, $regex);
$regex = !empty($regex[1]) ? $regex[1] : "FAIL";
echo $regex;

When I do echo $regex I always get FAIL how can I fix this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:34:11+00:00Added an answer on May 15, 2026 at 11:34 am

    As Aaron has suggested. Best not to reinvent the wheel so try parsing it with simplexml_load_string()

    // Init the CURL
    $curl = curl_init();
    
    // Setup the curl settings
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
    
    // grab the XML file
    $raw_xml = curl_exec($curl);
    
    curl_close($curl);
    
    // Setup the xml object
    $xml = simplexml_load_string( $raw_xml );
    

    You can now access any part of the $xml variable as an object, with that in regard here is an example of what you posted.

    <Response> 
        <Ip>74.125.45.100</Ip> 
        <Status>OK</Status> 
        <CountryCode>US</CountryCode> 
        <CountryName>United States</CountryName> 
        <RegionCode>06</RegionCode> 
        <RegionName>California</RegionName> 
        <City>Mountain View</City> 
        <ZipPostalCode>94043</ZipPostalCode> 
        <Latitude>37.4192</Latitude> 
        <Longitude>-122.057</Longitude> 
        <Timezone>0</Timezone> 
        <Gmtoffset>0</Gmtoffset> 
        <Dstoffset>0</Dstoffset> 
    </Response> 
    

    Now after you have loaded this XML string into the simplexml_load_string() you can access the response’s IP address like so.

    $xml->IP;
    

    simplexml_load_string() will transform well formed XML files into an object that you can manipulate. The only other thing I can say is go and try it out and play with it

    EDIT:

    Source
    http://www.php.net/manual/en/function.simplexml-load-string.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use a regex to get every variant of a method
I use a regex to get a value from a document and store it
I have been trying to use regex but I cant seem to get it
Edit: I'm really just curious as to how I can get this regex to
I would like to get the cid value of the url, in this case:
I would use a multi dimensional gaussian modell for regression. Rasmussen has a book
I would use Jmagick into an Android Project. Is it possible? I have to
I thought I would use a stored routine to clean up some of my
In .NET I would use System.Diagnostics.Trace... What would I use in C or C++
I just would like to know what format and codec would use the least

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.