示例数据是这样的:
<select name="ctl00$ContentPlaceHolder1$UrunListesi$ctrl0$StokBoyut" id="ctl00_ContentPlaceHolder1_UrunListesi_ctrl0_StokBoyut">
<option value="900061_50x80">50x80 Stok:0</option>
<option value="900067_60x110">60x110 Stok:0</option>
<option value="900037_70x140">70x140 Stok:0</option>
<option value="356593_80 T.Yolluk">80 T.Yolluk Stok:2</option>
<option value="356552_80x150">80x150 Stok:23</option>
</select>
使用以下代码,我只能捕获一个数据:
$regex_select = '/<select name="ctl00\$ContentPlaceHolder1\$UrunListesi\$ctrl([0-9]{1,2})\$StokBoyut" id="ctl00_ContentPlaceHolder1_UrunListesi_ctrl([0-9]{1,2})_StokBoyut">(.*?)/msi';
preg_match_all($regex_select, $get, $d);
如何捕获所有数据到“ SIZExSIZE”(例如50x80或150x230)和“股票:[0-9]”,直到最后一次选择。
答案 0 :(得分:1)
应该尝试使用DOMDocument
对象。
<?php
$html = '<select name="ctl00$ContentPlaceHolder1$UrunListesi$ctrl0$StokBoyut" id="ctl00_ContentPlaceHolder1_UrunListesi_ctrl0_StokBoyut">
<option value="900061_50x80">50x80 Stok:0</option>
<option value="900067_60x110">60x110 Stok:0</option>
<option value="900037_70x140">70x140 Stok:0</option>
<option value="356593_80 T.Yolluk">80 T.Yolluk Stok:2</option>
<option value="356552_80x150">80x150 Stok:23</option>
</select>';
$html = '<html>'. $html . '</html>';
/*** a new dom object ***/
$dom = new DOMDocument();
/*** load the html into the object ***/
$dom->loadHTML($html);
$pattern = '@\d+x\d+@s'; // Format: (number)x(number)
$stokPattern = '@(Stok:)\d+@s';
foreach ($dom->getElementsByTagName('option') as $row) {
$attributeValue = $row->getAttribute('value');
preg_match_all($pattern, $attributeValue);
if (preg_match($pattern, $attributeValue, $match)) {
echo $match[0] . '<br/>';
}
$text = $row->textContent;
if (preg_match($stokPattern, $text, $match)) {
echo $match[0] . '<br/>';
}
//echo $row->getAttribute('value') . '<br/>'; //Get value
//echo $row->textContent . '<br/>'; // Get text
}
示例结果:
50x80
Stok:0
60x110
Stok:0
70x140
Stok:0
Stok:2
80x150
Stok:23
答案 1 :(得分:0)
只需将(.*)
更改为([\s\S]*?)<\/select>
因此完整的正则表达式为:<select name="ctl00\$ContentPlaceHolder1\$UrunListesi\$ctrl([0-9]{1,2})\$StokBoyut" id="ctl00_ContentPlaceHolder1_UrunListesi_ctrl([0-9]{1,2})_StokBoyut">([\s\S]*?)<\/select>
.
与换行符不匹配,因此您可以使用[\s\S]
来代替任何内容。
一旦有了所需的select
。使用额外的正则表达式value=".*?_(\d+(?:x\d+)?)">.*?Stok:(\d+)
获取大小和库存值(第一和第二捕获组)
例如:
<?php
$re = '/<select name="ctl00\$ContentPlaceHolder1\$UrunListesi\$ctrl([0-9]{1,2})\$StokBoyut" id="ctl00_ContentPlaceHolder1_UrunListesi_ctrl([0-9]{1,2})_StokBoyut">([\s\S]*?)<\/select>/mi';
$str = '
<select name="ctl00$ContentPlaceHolder1$UrunListesi$ctrl0$StokBoyut" id="ctl00_ContentPlaceHolder1_UrunListesi_ctrl0_StokBoyut">
<option value="900061_50x80">50x80 Stok:0</option>
<option value="900067_60x110">60x110 Stok:0</option>
<option value="900037_70x140">70x140 Stok:0</option>
<option value="356593_80 T.Yolluk">80 T.Yolluk Stok:2</option>
<option value="356552_80x150">80x150 Stok:23</option>
</select>
<select>
<option value="dontwantthis">xxxx</option>
</select>
<select name="ctl00$ContentPlaceHolder1$UrunListesi$ctrl0$StokBoyut" id="ctl00_ContentPlaceHolder1_UrunListesi_ctrl0_StokBoyut">
<option value="900061_50x80">50x80 Stok:0</option>
<option value="900067_60x110">60x110 Stok:0</option>
<option value="900037_70x140">70x140 Stok:0</option>
<option value="356593_80 T.Yolluk">80 T.Yolluk Stok:2</option>
<option value="356552_80x150">80x150 Stok:23</option>
</select>
';
preg_match_all($re, $str, $matches);
foreach ($matches[0] as $select) {
print "I like this select:______________\n";
print $select . "\n";
print "The values are:**********\n";
preg_match_all('/value=".*?_(\d+(?:x\d+)?)[^"]*">.*?Stok:(\d+)/', $select, $values, PREG_SET_ORDER);
foreach ($values as $value) {
print " - $value[1] and Stock=$value[2]\n";
}
print "\n\n";
}
查看实际情况here。
结果
The values are:
- 50x80 and Stock=0
- 60x110 and Stock=0
- 70x140 and Stock=0
- 80 and Stock=2
- 80x150 and Stock=23