XPath 正则表达式与 preg_match 结合
我的代码中有一个简单但不可见的(对我来说)错误。你能帮助我吗?
在我的 php 文件中使用此代码:
$location = $xpath2->query("//script")->item(1)->textContent;
我得到(选择)此:
<script class="" type="text/javascript">
//<![CDATA[
var html = '';
var lat = 44.793530904744074;
var lang = 20.5364727973938;
if (GBrowserIsCompatible())
{
var map = new GMap2(document.getElementById("map_canvas"));
var ct = new GLatLng(lat, lang);
map.setCenter(ct, 15);
map.addControl( new GSmallMapControl() );
//map.addControl( new GHierarchicalMapTypeControl () );
var gm=new GMarker(ct);
if(html != '') {
GEvent.addListener(gm, "click", function() {
this.openInfoWindowHtml( html );
});
}
map.addOverlay(gm);
map.enableContinuousZoom();
map.enableInfoWindow();
}
//]]>
</script>
然后我尝试使用此代码获取“lat”和“lang”:
$location = $xpath2->query("//script")->item(1)->textContent;
preg_match('/var\s+lat\s+=\s+(\d+\.\d+)\s*;/', $location, $lat);
preg_match('/var\s+lang\s+=\s+(\d+\.\d+)\s*;/', $location, $lng);
$data['lat'] = $lat[1];
$data['lng'] = $lng[1];
但始终显示 lat
和 lang 是
0
、0
,而它们应该是 44.34534
和 20.5345
。
请帮忙!你认为我错的地方(我的英语不是很好,对此感到抱歉)
I have a simple but invisible (for me) error in code. Can you help me?
With this code in my php file:
$location = $xpath2->query("//script")->item(1)->textContent;
I got (select) this:
<script class="" type="text/javascript">
//<![CDATA[
var html = '';
var lat = 44.793530904744074;
var lang = 20.5364727973938;
if (GBrowserIsCompatible())
{
var map = new GMap2(document.getElementById("map_canvas"));
var ct = new GLatLng(lat, lang);
map.setCenter(ct, 15);
map.addControl( new GSmallMapControl() );
//map.addControl( new GHierarchicalMapTypeControl () );
var gm=new GMarker(ct);
if(html != '') {
GEvent.addListener(gm, "click", function() {
this.openInfoWindowHtml( html );
});
}
map.addOverlay(gm);
map.enableContinuousZoom();
map.enableInfoWindow();
}
//]]>
</script>
Then I try to fetch 'lat' and 'lang' with this code:
$location = $xpath2->query("//script")->item(1)->textContent;
preg_match('/var\s+lat\s+=\s+(\d+\.\d+)\s*;/', $location, $lat);
preg_match('/var\s+lang\s+=\s+(\d+\.\d+)\s*;/', $location, $lng);
$data['lat'] = $lat[1];
$data['lng'] = $lng[1];
But always show that lat
and lang
is 0
, 0
when they should be 44.34534
and 20.5345
.
PLEASE HELP! where you think that I'm wrong (my English is not very well, sorry for that)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许像下面这样。尽管您正在尝试解析 JavaScript,但要小心。
preg_match(
'/(?:^|(?<=\s))var\s+lat \s* = \s* (?=[^;]*\d) ([+-]? \d*\.?\d*)\s*; /x'
, $location, $lat);preg_match(
'/(?:^|(?<=\s))var\s+lang\s* = \s* (?=[^;]*\d) ([+-]? \d*\.?\d*)\s*; /x'
, $location, $lng);运行示例: http://www.ideone.com/SEgVb
或者,尝试获取更多一般信息:
preg_match(
'/(?:^|(?<=\s))var\s+lat \s*=\s* ([^;]*) \s*; /x', ...
Maybe something like below. Beware though you're trying to parse JavaScript.
preg_match(
'/(?:^|(?<=\s))var\s+lat \s* = \s* (?=[^;]*\d) ([+-]?\d*\.?\d*)\s*; /x'
, $location, $lat);preg_match(
'/(?:^|(?<=\s))var\s+lang\s* = \s* (?=[^;]*\d) ([+-]?\d*\.?\d*)\s*; /x'
, $location, $lng);Run sample: http://www.ideone.com/SEgVb
Or, just try to get more general information:
preg_match(
'/(?:^|(?<=\s))var\s+lat \s*=\s* ([^;]*) \s*; /x'
, ...尝试这样:
[\d.-]+ 匹配任何带有数字的组。或 - (纬度/经度可以为负)
Try like this:
The [\d.-]+ matches any group with numbers . or - (lat/lon can be negative)