PHP PCRE 解析 javascript 代码
我正在尝试解析生成地图的 JavaScript 代码,以便我可以获得它正在使用的坐标(以下示例中的前 2 个长值)。
示例:
new OpenLayers.LonLat(-9.2009313718432200, 38.6318891148480000), 9);
我正在使用的功能如下:
preg_replace('/LonLat\(([0-9 ,\-\.^\)]{0,})\)/i', '$1', $coords);
到目前为止没有成功。有什么建议吗?
I'm trying to parse a javascript code that generates a map, so that I can get the coordinates that it's using (the first 2 long values inside the following example).
The example :
new OpenLayers.LonLat(-9.2009313718432200, 38.6318891148480000), 9);
The function I'm using is as follows :
preg_replace('/LonLat\(([0-9 ,\-\.^\)]{0,})\)/i', '$1', $coords);
Got no success so far. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试像这样的正则表达式:
我的 PHP 有点生疏,所以 YMMV。匹配浮点数的表达式比它需要的要复杂得多,只需一个简单的“可选符号后跟一些数字,后跟小数点,后跟更多数字”就足够了:
并且不要忘记跳过 < code>$matches[2] 和
$matches[4]
,它们将包含内部捕获组(即“.20093137...”和“.6318891...”你的例子)。对于这样的事情,不要为
preg_replace()
烦恼,你只是在寻找东西,你并没有试图改变任何东西。您可以使用preg_replace()
但随后您必须再次解析替换后的值才能取出这两部分。Try a regex something like this:
My PHP is a bit rusty so YMMV. Your expression for matching a floating point number is far more convoluted that it needs to be, just a simple "optional sign followed by some digits followed by a decimal point followed by some more digits" is sufficient:
And don't forget to skip
$matches[2]
and$matches[4]
, those will contain the inner capture groups (i.e. ".20093137..." and ".6318891..." in your example).And don't bother with
preg_replace()
for things like this, you're just looking for things, you're not trying to change anything. You could usepreg_replace()
but then you'd have to parse the post-replacement value again to get the two pieces out.