how to “regex” all options in select menu
Clash Royale CLAN TAG#URR8PPP
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 50x80 or 150x230) and "Stock: [0-9]" until the last Select.
...something something html & regex something... stackoverflow.com/a/1732454/9197799
– DaGroove
Aug 10 at 6:35
2 Answers
2
Just change (.*)
by ([sS]*?)</select>
(.*)
([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>
<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.
.
[sS]
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)
select
value=".*?_(d+(?:xd+)?)">.*?Stok:(d+)
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
Thank you. Is there a way to extract the data in Option?
– Muhsin İşsever
Aug 10 at 7:09
I updated my answer for adding an example on how to extract the values
– Julio
Aug 10 at 7:53
updated to get size and stock values
– Julio
Aug 10 at 8:05
Should try with DOMDocument
object.
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+xd+@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
Example result:
50x80
Stok:0
60x110
Stok:0
70x140
Stok:0
Stok:2
80x150
Stok:23
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
why would you want to use regexp, when using DOMDocument would be so much easier ?
– Lars Stegelitz
Aug 10 at 6:29