Php 根据内部变量从javascript标记中刮取值

Php 根据内部变量从javascript标记中刮取值,php,xpath,web-scraping,Php,Xpath,Web Scraping,我正在编写一个PHP脚本来获取一些javascript变量值(地理坐标),这些值可以在单个标记中找到 <script> .... myFirstVariableValue = 58.922; mySecondVariableValue = -12.179; .... myLastVariableValue = 45.425; .... </script> 因此,我希望通过xpath查询找到正确的脚本标记,然后仅对脚本标记内容运行正则表达式 问题是xpath查询:甚至可以

我正在编写一个PHP脚本来获取一些javascript变量值(地理坐标),这些值可以在单个
标记中找到

<script>
....
myFirstVariableValue = 58.922;
mySecondVariableValue = -12.179;
....
myLastVariableValue = 45.425;
....
</script>
因此,我希望通过xpath查询找到正确的脚本标记,然后仅对脚本标记内容运行正则表达式


问题是xpath查询:甚至可以这样做吗?

经过大量尝试,我终于找到了正确的查询,而且非常简单

首先,我在脚本文本内容上使用contains获得正确的脚本

$script = $xpath->query("//script/text()[contains(.,'myFirstVariableValue')]");
之后,我可以使用
$script
变量来限制正则表达式的范围

preg_match("myFirstVariableValue\ =\ (-?\d+.\d+)/",$script->item(0)->textContent,$matches);
preg_match("mySecondVariableValue\ =\ (-?\d+.\d+)/",$script->item(0)->textContent,$matches);
...
preg_match("myLastVariableValue\ =\ (-?\d+.\d+)/",$script->item(0)->textContent,$matches);
preg_match("myFirstVariableValue\ =\ (-?\d+.\d+)/",$script->item(0)->textContent,$matches);
preg_match("mySecondVariableValue\ =\ (-?\d+.\d+)/",$script->item(0)->textContent,$matches);
...
preg_match("myLastVariableValue\ =\ (-?\d+.\d+)/",$script->item(0)->textContent,$matches);