php - 用PHP提取两个字符串之间的数据
问题描述
我有以下字符串:
$html = '"id":75549,"name":"Name","lat":"45.491834","lng":" -73.606953","address"';
我想提取lat
和lng
数据。
这是我的尝试:
$lat = preg_match_all('#lat":"(.*?)","lng#', $html, $matches);
$lat = matches[1];
但它不起作用。
你能帮帮我吗?
谢谢。
解决方案
json_decode
比正则表达式可靠得多。为缺少的"address"
元素添加大括号和值,您可以直接索引到结果中:
<?php
$html = '"id":75549,"name":"Name","lat":"45.491834","lng":" -73.606953","address"';
$decoded = json_decode('{'.$html.':""}', true);
echo "lat: ".$decoded["lat"]." lng: ".$decoded["lng"];
输出:
lat: 45.491834 lng: -73.606953
推荐阅读
- html - 带有溢出滚动的固定位置 Div 在 Safari 中不可见
- ios - 用户登录一次后,我在设置初始化视图控制器时遇到问题
- c++ - 为什么我可以在 MSVC 和 icc 中为 glm::vec 创建用户定义的结构化绑定,但不能在 Clang 和 GCC 中创建?
- asp.net - 如何在 asp.net mvc 中更新图像?
- powershell - Powershell更改viso容器标题颜色和其他属性
- pandas - 熊猫排序可能的组合
- java - 如何使用 selenium、java 下载文件
- google-colaboratory - 谷歌 Colab 中的 CNN
- python - 替换文本文件中单词后面的数字
- angular - 如何在不使用按钮单击的情况下以 Angular 7 打开模式,