0

いくつかの 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>
4

3 に答える 3

4

それを配列に変換してから、次を使用して名前を取得できますarray_keys();

$array = json_decode($json);

$keys = array_keys($array['products']);
于 2013-05-31T12:28:47.253 に答える
1

なぜ、正規表現?ここで他の人が述べたように、json_decode を使用して配列に解析し、処理することができます。

ただし、正規表現を主張する/"(.+?)":/場合は、JSON が示されているとおりの正確な形式であれば、すべてのキーに一致すると言えます。

アップデート

したがって、html文字列から取得しています。変数が $html であり、正規表現を主張する場合、次のように正規表現を使用して json を解析し、デコードします。キーを解析するには、次を使用しますarray_keys()

preg_match('/<span.*?class="JSON".*?>(.+?)<\/span>/s', $html, $matches);

$decoded_array = json_decode($matches[1], true);

print_r($decoded_array);

$keys = array_keys($decoded_array['products'][0]);

print_r($keys);
于 2013-05-31T12:38:29.383 に答える