我正在阅读此链接的源代码Source
我有以下Javascript代码
<script type="text/javascript">
jQuery && jQuery(function() {
setTimeout(function() {
DL.items_list.initItemAttr(4, '{"sku-SV023435_B_M":"e90dfb84e30edf611e326eeb04d680de6c2de33453d563b7965b97195e855512","sku-SV023435_BL_M":"9594eec95be70e7b1710f730fdda33d96c2de33453d563b7965b97195e855512","sku-SV023435_PU_M":"b9ba865fec061c9706d2fd7ce49c0cc76c2de33453d563b7965b97195e855512"}', '{"price":"5.07","products_dropship_price":"0.0000","has_discount_price":false}', "", '{"sku-SV023435_B_M":7,"sku-SV023435_BL_M":10,"sku-SV023435_PU_M":11}', '{"image_e90dfb84e30edf611e326eeb04d680de":"http:\/\/img1.dresslink.com\/SV\/0\/SV023435-8484-SV023435-G.jpg","image_9594eec95be70e7b1710f730fdda33d9":"http:\/\/img1.dresslink.com\/SV\/0\/SV023435-8484-SV023435-G.jpg","image_b9ba865fec061c9706d2fd7ce49c0cc7":"http:\/\/img1.dresslink.com\/SV\/0\/SV023435-8484-SV023435_PU-G.jpg"}');
}, 2000);
var de = $('#w_pro_detail');
de.find('#switchItem').click(function(){
var _t = $(this);
if(_t.is('.narrow')){
var p_wh = _t.parent().width();
_t.attr('class','expend').text('>>').parent().animate({left:0,opacity:1},500).css({opacity:1,paddingRight:20});
_t.parent().find('.p_info,.size_color,.quantity').hide();
de.css('min-width','0');
UtilTool.setCookie('isMiniQuickShop', '1', 24);
}else{
_t.attr('class','narrow').text('<<').parent().animate({left:10},500);
_t.parent().find('.p_info,.size_color,.quantity').show();
de.css('min-width','480px');
UtilTool.setCookie('isMiniQuickShop', '', 0);
}
}).end().mouseover(function(event){
var elem = $(event.currentTarget),
fromElem = $(event.target);
if(elem.has(fromElem).length || fromElem.is(elem)){
de.stop();
de.animate({opacity:1},500);
}
}).mouseout(function(event){
var elem = $(event.currentTarget),
toElem = $(event.target);
if(!elem.has(toElem).length || toElem.is(elem)){
de.stop();
de.animate({opacity:0.15},500);
}
});
var isMiniQuickShop = UtilTool.getCookie('isMiniQuickShop');
if(isMiniQuickShop == '1') {
de.css('min-width','0');
$('#w_pro_detail').find('#switchItem').text('>>').attr('class','expend').end().css({opacity:1,left:0,paddingRight:20}).find('.p_info,.size_color,.quantity').hide();
}
});
</script>
在下面的代码中,json部分对我很重要,它有每个SKU的库存可用性。
'{"sku-SV023435_B_M":7,"sku-SV023435_BL_M":10,"sku-SV023435_PU_M":11}'
现在请帮助我如何使用PHP和preg_match或任何合适的函数来获取此部分。
谢谢!
更新: 数字os SKU不相同,它不同于一个。这是另一个样本:
'{"sku-11430_B_S":"20","sku-11430_B_M":"17","sku-11430_B_L":"30","sku-11430_B_XS":"13","sku-11430_BL_S":"7","sku-11430_BL_M":"17","sku-11430_BL_L":"4","sku-11430_BL_XS":"16","sku-11430_O_S":"8","sku-11430_O_M":"6","sku-11430_O_L":"22","sku-11430_O_XS":"20","sku-11430_LBL_S":"27","sku-11430_LBL_M":"25","sku-11430_LBL_L":"22","sku-11430_LBL_XS":"10","sku-11430_Y_S":"24","sku-11430_Y_M":36,"sku-11430_Y_L":"20","sku-11430_Y_XS":"6","sku-11430_RR_S":"4","sku-11430_RR_M":"35","sku-11430_RR_L":"47","sku-11430_RR_XS":"6"}',
答案 0 :(得分:0)
我建议先使用preg_match_all
提取这些子字符串,然后使用json
进行解析。
我假设你感兴趣的JSON对象总是
sku-
提取正则表达式将如下所示:
$re = "#\{(?:,?\"sku-\w+\":\"?\d+\"?)+\}#i";
Code:
$re = "#\{(?:,?\"sku-\w+\":\"?\d+\"?)+\}#i";
$str = "<<<YOUR_STRING>>>";
preg_match_all($re, $str, $matches);
foreach ($matches[0] as $val)
{
$a = $val;
$a = json_decode($a);
print_r($a);
}
输出:
stdClass Object
(
[sku-11430_B_S] => 20
[sku-11430_B_M] => 17
[sku-11430_B_L] => 30
[sku-11430_B_XS] => 13
[sku-11430_BL_S] => 7
[sku-11430_BL_M] => 17
[sku-11430_BL_L] => 4
[sku-11430_BL_XS] => 16
[sku-11430_O_S] => 8
[sku-11430_O_M] => 6
[sku-11430_O_L] => 22
[sku-11430_O_XS] => 20
[sku-11430_LBL_S] => 27
[sku-11430_LBL_M] => 25
[sku-11430_LBL_L] => 22
[sku-11430_LBL_XS] => 10
[sku-11430_Y_S] => 24
[sku-11430_Y_M] => 36
[sku-11430_Y_L] => 20
[sku-11430_Y_XS] => 6
[sku-11430_RR_S] => 4
[sku-11430_RR_M] => 35
[sku-11430_RR_L] => 47
[sku-11430_RR_XS] => 6
)
stdClass Object
(
[sku-SV023435_B_M] => 7
[sku-SV023435_BL_M] => 10
[sku-SV023435_PU_M] => 11
)
这个正则表达式更安全一些,因为它可以匹配sku-
部分中的转义实体:
$re = "#\{(?:,?\s*\"sku-[^\"\\]*(?:\\.[^\"\\]*)*\"\s*:\s*\"?\d+\"?\s*)+\}#i";
提取所有JSON对象:
$re = "#\{\s*\"[^\"\\]*(?:\\.[^\"\\]*)*\"\s*:\s*(?:\"[^"\\]*(?:\\.[^\"\\]*)*\"|\S+)\s*\}#";