【问题标题】:issues using preg_match to get data from xml file使用 preg_match 从 xml 文件获取数据的问题
【发布时间】:2011-12-16 20:07:48
【问题描述】:

我正在尝试使用外部站点的 API 通过file_get_contents 获取用户 IP 信息(国家、城市等):

$ht="http://api.hostip.info/?ip=99.99.99.99";//example ip address<<
$com = file_get_contents("$ht");

preg_match('/<countryName>(.*)<\/countryName>/',$com,$oun);
preg_match('/<gml:name>(.*)<\/gml:name>/',$com,$it);
$country=$oun[1];
$city=$it[1];
echo $country, $city;

我可以使用该代码获取国家、IP 和其他信息,但不能获取城市...

我认为问题在于

gml:name....

这是我试图从中获取数据的 XML:

http://api.hostip.info/

【问题讨论】:

  • 使用适当的 XML 解析器代替正则表达式怎么样?
  • 为什么不用simplexml来阅读呢?

标签: php preg-match file-get-contents


【解决方案1】:

你需要转义冒号'/&lt;gml\:name&gt;(.*)&lt;\/gml\:name&gt;/'

【讨论】:

  • 我在 cli 中进行了一次运行,这对我有用。 PHP 版本 5.3.3.7,preg_match('/&lt;gml\:name&gt;(.*)&lt;\/gml\:name&gt;/', '&lt;gml:name&gt;hostip&lt;/gml:name&gt;', $res);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多