PHP preg模式需要从文本中获取所需的数据

时间:2015-04-26 21:36:22

标签: php regex preg-match

我需要阅读在线时尚商店的HTML源代码。每种产品都有不同的尺寸,每种尺寸都有特定的数量,定义如下:

<script>
    LMS.pageData['product']['variantSizes'].push({
        optionsString: 'XL',
        selected : 'false',
        selectedSize : '',
        optionsStringForDisplay: 'XL',
        valueString: 'AED@30@true@156231824@40@30@50.0@20@2',
        sizeVariantPromotionPercentage: '40',
        newSizePrice: '30',
        oldSizePrice: '50.0',
        sizeSavedAmount: '20',
        codeForUrl: '6014989',
        variantSize: JSON.parse("{\"stockLevelStatus\":{\"code\":\"inStock\",\"type\":\"StockLevelStatus\"},\"stockLevel\":2,\"url\":\"\/Women\/Regular\/Tops\/T-Shirts-%26-Vests\/Drop-Shoulder-Printed-Top\/p\/156231824\",\"priceData\":{\"currencyIso\":\"AED\",\"value\":30,\"priceType\":\"BUY\",\"formattedValue\":\"AED50.00\"},\"variantOptionQualifiers\":[{\"qualifier\":\"size\",\"name\":null,\"value\":\"XL\",\"image\":null}],\"code\":\"156231824\",\"variantType\":null,\"lmgSizeVariantOptionData\":null,\"potentialPromotions\":[{\"code\":\"PFPPWTS S15 190-TOPS-SSP15\",\"promotionType\":\"Fixed price\",\"endDate\":1435694400000,\"description\":\"PFPP\",\"couldFireMessages\":null,\"firedMessages\":null,\"productBanner\":null,\"percentageDiscount\":40,\"discountedPrice\":30.0,\"savedAmount\":20,\"title\":\"PFPP\",\"voucherAmount\":null}],\"basePrice\":{\"currencyIso\":\"AED\",\"value\":50.0,\"priceType\":\"BUY\",\"formattedValue\":\"AED50.00\"}}"),
    });
</script>  

所以这部分对我们很重要

optionsStringForDisplay: 'XL',
valueString: 'AED@30@true@156231824@40@30@50.0@20@2',

尺码为XL,此尺码的数量为2(最后一个@之后的数字)

现在请帮我写一个PHP代码来捕获这些数据。我的意思是我需要知道XL的数量。

2 个答案:

答案 0 :(得分:0)

当你解析了valueString时,正则表达式为:

$valueString ='AED@30@true@156231824@40@30@50.0@20@2';
preg_match('#.*@(.*?)$#', $valueString, $matches);
echo $matches[1];

编辑:如果下载内容,则:

$text = file_get_content($url);
preg_match_all('#.*@(\d+)#', $text, $matches);
var_dump($matches[1]); // all matches near XS, S descriptions

输出:

array(2) {
  [0]=>
  string(1) "5"
  [1]=>
  string(1) "2"
}

编辑编辑 测试数据:

$text = "<script>
            selectedSize : '',
            optionsStringForDisplay: 'L',
            valueString: 'AED@30@true@156231823@40@30@50.0@20@5',
            sizeVariantPromotionPercentage: '40',
            selectedSize : '',
            optionsStringForDisplay: 'XL',
            valueString: 'AED@30@true@156231824@40@30@50.0@20@2',
            sizeVariantPromotionPercentage: '40',
            codeForUrl: '6014989',
";
preg_match_all('#optionsStringForDisplay: \'(.*?)\',\s*?.*@(\d+)#m', $text, $matches);
var_dump($matches[1]);
var_dump($matches[2]);

结果:

array(2) {
  [0]=>
  string(1) "L"
  [1]=>
  string(2) "XL"
}
array(2) {
  [0]=>
  string(1) "5"
  [1]=>
  string(1) "2"
}

合并结果:

 $new_matches = array();
 foreach ($matches[1] as $key => $sizes) {
      $new_matches[] = array("size" => $matches[2][$key], "value" => $matches[1][$key]);
 }

输出:

array(2) {
  [0]=>
  array(2) {
    ["size"]=>
    string(1) "5"
    ["value"]=>
    string(1) "L"
  }
  [1]=>
  array(2) {
    ["size"]=>
    string(1) "2"
    ["value"]=>
    string(2) "XL"
  }
}

答案 1 :(得分:0)

这应该这样做:

preg_match_all('/optionsStringForDisplay: \'(.*?)\'.*valueString:.*@(\d+)/sim', $html, $matches, PREG_PATTERN_ORDER);
$optionsStringForDisplay = $matches[1][0];
$valueString= $matches[2][0];

样本

http://ideone.com/ixaq3W