dtuy84033 2014-10-10 10:09
浏览 55
已采纳

在HTML页面上的javascript函数中获取嵌入值

I am using php and trying to fetch a value in a javascript function. The javascript in the HTML page is the following:

<script type="text/javascript">

        function show(q)
        {
            if(q.length > 0)
            {           
                if(q.indexOf("tid") > -1)
                {
                    location.href = "map.jsp?" + q;
                }
                else
                {
                    location.href = "listmap.jsp?" + q;
                }
            }

            return false;
        }

        $(function() { 
            $("#map").goMap({ 
                latitude: -35.331415
                ,longitude: 149.131851
                ,maptype: 'ROADMAP'
                ,scrollwheel: false
                ,zoom: 11
                ,markers: [ {latitude: -35.192103,longitude: 149.332313,icon: 'images/m/marker1.png',html: {content: 'Loading...', ajax: 'mapinfo.jsp?sid=9057'}}]

I need to get the values of (3rd and 4th lines in second function) the latitude and longitude which are: -35.331415 and 149.131851

I'm just not sure how to access the individual values...

EDIT: I AM USING PHP to save these values in variables then later write them to a csv file Currently i'm using this code (here's a snippit) to get everything else on other pages

<?php


$ch = curl_init("http://pvoutput.org/listmap.jsp?sid=312");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cl = curl_exec($ch);


$dom = new DOMDocument();
@$dom ->loadHTML($cl);

$Lat_data = $dom -> getElementsByTagName("script");
print_r($Lat_data -> item(1) );

EDIT 2: I fixed all the problems by doing this:

$Lat_data = $dom -> getElementsByTagName("script");
$content =$Lat_data -> item(10) -> textContent;

$regex = "/latitude: ([^\b,]*)/"; 
if (preg_match( $regex , $content , $values )) {
echo  $values[1];
$regex2 = "/longitude: ([^\b,]*)/";
}
if (preg_match($regex2, $content, $values2)) {
echo $values2[1];
}
  • 写回答

2条回答 默认 最新

  • dpw43061 2014-10-10 10:40
    关注

    You managed to get the script node. Now you want its content (the javascript code)

    $content = $Lat_data -> item(1) -> textContent
    

    Once you got the content you can parse it using regex expression to get what you want

    $regex = "/latitude: ([^\b
    ]*)(.|
    )*,longitude: ([^\b
    ,]*)/";
    if (preg_match($regex, $content, $values)) {
       echo 'latitude: ' . $values[0];
       echo 'longitue: ' . $values[2];
    }
    

    regex explanation (test here):

    • finds "latitude: "
    • then we get the first group (), [^\b ]* means anything but blackspace or newline characters
    • get anything between latitude value and ',longitude: '
    • next find ",longitude: "
    • third group just as the previous one. I added ',' so that if there is not blank between the longitude and ', maptype' it still works.

    this regex explanation is getting a little messy, check the test link to more detailed explanation.

    I have not tested it so there might be some syntax error but the idea is there ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作