Note that there are some explanatory texts on larger screens.

plurals
  1. POpreg match for xml output node
    primarykey
    data
    text
    <p>I have the following script that uses the api on <em>hostip.info</em>. The page parses an xml readout of a user location based on the ip address. In my function everything is working except for the city. </p> <pre><code>preg_match("@&lt;Hostip&gt;(\s)*&lt;gml:name&gt;(.*?)&lt;/gml:name&gt;@si",$xml,$city_match); </code></pre> <p>I have narrowed it down to my <code>preg_match</code> being wrong but I'm not sure how to fix it. Here is a sample xml output: <a href="http://api.hostip.info/?ip=12.215.42.19" rel="nofollow">http://api.hostip.info/?ip=12.215.42.19</a></p> <pre><code>&lt;?php function getCountryCity() { if(isset($_SERVER['REMOTE_ADDR']) &amp;&amp; strlen($_SERVER['REMOTE_ADDR']) &gt; 0) { $ipAddr = $_SERVER['REMOTE_ADDR']; // verify the IP address ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : ""; $ipDetail=array(); // get the XML result from hostip.info $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr); // get the city name inside the node &lt;gml:name&gt; and &lt;/gml:name&gt; preg_match("@&lt;Hostip&gt;(\s)*&lt;gml:name&gt;(.*?)&lt;/gml:name&gt;@si",$xml,$city_match); $ipDetail['city'] = $city_match[1]; // get the country name inside the node &lt;countryName&gt; and &lt;/countryName&gt; preg_match("@&lt;countryName&gt;(.*?)&lt;/countryName&gt;@si",$xml,$country_match); $ipDetail['country'] = $country_match[1]; // get the country name inside the node &lt;countryName&gt; and &lt;/countryName&gt; preg_match("@&lt;countryAbbrev&gt;(.*?)&lt;/countryAbbrev&gt;@si",$xml,$cc_match); $ipDetail['country_code'] = $cc_match[1]; // return the array containing city, country and country code return $ipDetail; } else { return false; } } $ipDetail = getCountryCity(); $user_city = $ipDetail['city']; $user_country = $ipDetail['country']; $user_cc = $ipDetail['country_code']; echo $user_country.' ('.$user_cc.')'; echo $user_city; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload