gpt4 book ai didi

php - 如何在 php 中使用正则表达式 (regex) 从 json 获取内容

转载 作者:行者123 更新时间:2023-11-28 13:42:35 24 4
gpt4 key购买 nike

我需要提取一些 html 标签中存在的 json。如何使用正则表达式从下面的 json 中提取名称(键)值

<div id="gwt_products_display_results" class="gwt_products_display_results">
<span class="JSON" style="display: none;">
{
"products": [
{
"targetURL": "/athena-mineral-fabric-by-the-yard/262682",
"listIndex": "0",
"minimumPrice": 20,
"categoryOnSale": "false",
"mfPartNumber": "FF010ATM",
"hasAtLeastOneBuyableAndPublishedItem": "true",
"attributes": [],
"partNumber": "b_FF010ATM",
"itemAsProduct": "true",
"iapAttribute": "",
"productDetailTargetURL": "/athena-mineral-fabric-by-the-yard/262682",
"iapAttributeCode": "",
"beanType": "bundle",
"name": "Athena Mineral Fabric by the Yard",
"maxListPrice": 0,
"thumbNail": "null",
"hasSaleSKUs": false,
"productId": "262682",
"currencyCode": "USD",
"hasMoreColors": false,
"xPriceLabel": "null",
"minListPrice": 0,
"maximumPrice": 20,
"iapAttributeDisplayName": "",
"shortDescription": "null",
"listId": "SEARCHRESULTS",
"categoryId": "null"
},
{
"targetURL": "/athena-slate-fabric-by-the-yard/262683",
"listIndex": "1",
"minimumPrice": 20,
"categoryOnSale": "false",
"mfPartNumber": "FF010ATS",
"hasAtLeastOneBuyableAndPublishedItem": "true",
"attributes": [],
"partNumber": "b_FF010ATS",
"itemAsProduct": "true",
"iapAttribute": "",
"productDetailTargetURL": "/athena-slate-fabric-by-the-yard/262683",
"iapAttributeCode": "",
"beanType": "bundle",
"name": "Athena Slate Fabric by the Yard",
"maxListPrice": 0,
"thumbNail": "null",
"hasSaleSKUs": false,
"productId": "262683",
"currencyCode": "USD",
"hasMoreColors": false,
"xPriceLabel": "null",
"minListPrice": 0,
"maximumPrice": 20,
"iapAttributeDisplayName": "",
"shortDescription": "null",
"listId": "SEARCHRESULTS",
"categoryId": "null"
},
{
"targetURL": "/typewriter-keys-giclee/261307",
"listIndex": "2",
"minimumPrice": 259,
"categoryOnSale": "false",
"mfPartNumber": "WD813",
"hasAtLeastOneBuyableAndPublishedItem": "true",
"attributes": [
{
"S7 - Overlay 1": "blank"
}
],
"partNumber": "p_WD813",
"itemAsProduct": "true",
"iapAttribute": "",
"productDetailTargetURL": "/typewriter-keys-giclee/261307",
"iapAttributeCode": "",
"beanType": "product",
"name": "Typewriter Keys Giclee",
"maxListPrice": 0,
"thumbNail": "null",
"hasSaleSKUs": false,
"productId": "261307",
"currencyCode": "USD",
"hasMoreColors": false,
"xPriceLabel": "null",
"minListPrice": 0,
"maximumPrice": 259,
"iapAttributeDisplayName": "",
"shortDescription": "null",
"listId": "SEARCHRESULTS",
"categoryId": "null"
}
]
}
</span>
</div>

到目前为止我尝试过的是

<span class="JSON" style="display: none;">([\s\S]+?)<\/span>

最佳答案

您可以将其转换为数组,然后使用 array_keys(); 获取名称

$array = json_decode($json);

$keys = array_keys($array['products']);

关于php - 如何在 php 中使用正则表达式 (regex) 从 json 获取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16857279/

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