douliang2087 2017-04-30 13:51
浏览 27

php谷歌地图正则表达式preg_match

i have this data

  var companies = [{"name":"Beliaa","lat":"30.043438999999999","lng":"31.239159999999998","infowindow":{"title":"Beliaa","address":"28 El Falaky St., Bab El Louk, Downtown, Cairo"}}];
translator.add("Rated Successfully", "Rated Successfully");
translator.add("Reviewed Successfully", "Your review has been submitted and will be published as soon as possible. Thanks for sharing!");

i want to get

lat:30.043438999999999 and lng:31.239159999999998

with preg_match

  • 写回答

2条回答 默认 最新

  • dsgk0386 2017-04-30 14:03
    关注

    If you try to parse JS code in PHP

    $subject = <<<'__EOS__'
     var companies = [{"name":"Beliaa","lat":"30.043438999999999","lng":"31.239159999999998","infowindow":{"title":"Beliaa","address":"28 El Falaky St., Bab El Louk, Downtown, Cairo"}}];
    translator.add("Rated Successfully", "Rated Successfully");
    translator.add("Reviewed Successfully", "Your review has been submitted and will be published as soon as possible. Thanks for sharing!");
    __EOS__;
    
    
    if(preg_match('/"lat":"(.*?)"/', $subject, $matches))
      echo "lat:{$matches[1]}";
    else
      echo 'not found';
    

    Since your source string is a very simple snippet, you can keep your regular expression simple as well. The searched string is surrounded by the literal "lat":"...". The parenthesis define a subpattern (.*?) which is captured in $matches[1]. The dot means "any character", the asterisk is a quantifier meaning 0 or more times, the questionmark makes the quantified expression ungreedy, so it stops on the next matched pattern behind this expression (the quotes in this case). Otherwise you would get the entire string until sharing!" (the last quotes found).

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么