gpt4 book ai didi

PHP:在字符串中的特定单词之后提取特定单词?

转载 作者:行者123 更新时间:2023-12-01 22:26:44 25 4
gpt4 key购买 nike

我有一个看起来像这样的字符串:

{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}

我只需要该字符串中 country_name 的值。

所以我试了一下:

$country = '{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}';

if (preg_match('#^country_name: ([^\s]+)#m', $country, $match)) {
$result = $match[1];
}

echo $result;

$result

中没有任何回显

有人可以就此问题提出建议吗?

最佳答案

$country = json_decode('{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}');

echo $country->country_name;

你有一个 JSON 字符串。

JSON 代表 JavaScript 对象表示法。PHP 可以通过 json_decode($string, FALSE); 将其解码为数组或对象;

默认情况下,第二个参数是 FALSE,这意味着它将把字符串转换成一个对象,然后您可以像我上面展示的那样访问它。

关于PHP:在字符串中的特定单词之后提取特定单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33371901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com