从文本中获取所需数据所需的PHP preg模式
我需要阅读一个网上时装店的HTML源代码。每种产品都有不同的尺寸,每种尺寸都有特定的数量,定义如下:从文本中获取所需数据所需的PHP preg模式,php,regex,preg-match,Php,Regex,Preg Match,我需要阅读一个网上时装店的HTML源代码。每种产品都有不同的尺寸,每种尺寸都有特定的数量,定义如下: <script> LMS.pageData['product']['variantSizes'].push({ optionsString: 'XL', selected : 'false', selectedSize : '', optionsStringForDisplay: 'XL', va
<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>
尺寸为XL,此尺寸的数量为2(最后@后的数字)
现在请帮助我编写一个PHP代码来捕获这些数据。我的意思是我需要知道尺寸XL的数量。当您解析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"
}
array(2) {
[0]=>
array(2) {
["size"]=>
string(1) "5"
["value"]=>
string(1) "L"
}
[1]=>
array(2) {
["size"]=>
string(1) "2"
["value"]=>
string(2) "XL"
}
}
编辑的编辑
数据测试:
$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]);
合并结果:
$new_matches = array();
foreach ($matches[1] as $key => $sizes) {
$new_matches[] = array("size" => $matches[2][$key], "value" => $matches[1][$key]);
}
输出:
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "2"
}
array(2) {
[0]=>
array(2) {
["size"]=>
string(1) "5"
["value"]=>
string(1) "L"
}
[1]=>
array(2) {
["size"]=>
string(1) "2"
["value"]=>
string(2) "XL"
}
}
这应该做到:
preg_match_all('/optionsStringForDisplay: \'(.*?)\'.*valueString:.*@(\d+)/sim', $html, $matches, PREG_PATTERN_ORDER);
$optionsStringForDisplay = $matches[1][0];
$valueString= $matches[2][0];
演示
在
@
上拆分并获取最后一部分。您能更清楚地解释一下吗?请注意,全文中不仅仅只有一个valueString,例如,如果有5个大小,我们将有5个valueString,请查看下面的地址,我们需要从这里捕获数据:它工作得很好!但对于我这个不太专业的PHP人来说,这还不够,我的目标是捕获所有大小的数据并相应地更新我的网站,所以我更喜欢检索一个大小和数量组合的数组。它工作得很好,现在还有另一种情况,产品没有大小,在这种情况下,我们有如下:LMS.pageData['product']['maxThreshold']='6';你能帮我做这个吗?谢谢@jskidie@HamidJoukar你能举个例子吗?