Skip to content
Advertisement

how to “regex” all options in select menu

The sample data is this:

<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>

With the following code I can capture only one data:

$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);

How can I capture all data to “SIZExSIZE” (e.g 50×80 or 150×230) and “Stock: [0-9]” until the last Select.

Advertisement

Answer

Just change (.*) by ([sS]*?)</select>

so the complete regexp would be: <select name="ctl00$ContentPlaceHolder1$UrunListesi$ctrl([0-9]{1,2})$StokBoyut" id="ctl00_ContentPlaceHolder1_UrunListesi_ctrl([0-9]{1,2})_StokBoyut">([sS]*?)</select>

Demo

. does not match new lines so you may use [sS] instead which matches anything.

Once you have the select you want. Use an extra regex value=".*?_(d+(?:xd+)?)">.*?Stok:(d+) for getting the size and stock values (first and second capturing group)

For example:

<?php
$re = '/<select name="ctl00$ContentPlaceHolder1$UrunListesi$ctrl([0-9]{1,2})$StokBoyut" id="ctl00_ContentPlaceHolder1_UrunListesi_ctrl([0-9]{1,2})_StokBoyut">([sS]*?)</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+(?:xd+)?)[^"]*">.*?Stok:(d+)/', $select, $values, PREG_SET_ORDER);
    foreach ($values as $value) {
        print "  - $value[1] and Stock=$value[2]n";
    }
    print "nn";
}

See it in action here.

Result

The values are:
  - 50x80 and Stock=0
  - 60x110 and Stock=0
  - 70x140 and Stock=0
  - 80 and Stock=2
  - 80x150 and Stock=23
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement