Pattern matching without adding an array with preg_match_all
I'm trying to grab table text from an element that looks like this:
<span id="ctl00_MainContent_ListView2_ctrl2_ctl01_Label17" class="vehicledetailTable" style="display:inline-block;width:475px;">OWNED</span><br />
My preg_match_all looks like this:
preg_match_all('~475px;">(.*?)</span><br />~', $ret, $vehicle);
The problem is that there are other tables on the page that also match, but have data that is not relevant to my query. The data I want is in " ListView2 " but " ct101_Label17 " changes - Label18, Label19, Label20, etc.
Since I'm not interested in recording the label, is there a method to match the subject string without capturing the match? Something like:
<span id="ctl00_MainContent_ListView2_ctrl2_ctl01_[**WILDCARD HERE**]" class="vehicledetailTable" style="display:inline-block;width:475px;">OWNED</span><br />
Any help would be greatly appreciated.
source to share
Here is a very bad solution you are currently considering:
<span\b[^<>]*\bid="ctl00_MainContent_ListView2_ctrl2_ctl01_[^"]*"[^<>]*475px;">(.*?)</span><br\s*/>
Watch the demo
This makes sure we found the tag <span>
, and there is an attribute id
starting with ctl00_MainContent_ListView2_ctrl2_ctl01_
, and there is some attribute (and you know what it is style
) ending with 475px;
, and then we just grab everything up to the closing tag </span>
.
You can get this with DOM and XPath, which is a much safer solution that uses the same logic as above:
$html = "<span id=\"ctl00_MainContent_ListView2_ctrl2_ctl01_Label17\" class=\"vehicledetailTable\" style=\"display:inline-block;width:475px;\">OWNED</span><br />";
$dom = new DomDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$spans = $xpath->query("//span[starts-with(@id,'ctl00_MainContent_ListView2_ctrl2_ctl01_') and @class='vehicledetailTable' and contains(@style,'475px;')]");
$data = array();
foreach ($spans as $span) {
array_push($data, $span->textContent);
}
print_r($data);
Output: [0] => OWNED
Note that the XPath expression contains 3 conditions, feel free to change any of them:
-
//span
- get all the span tags that -
starts-with(@id,'ctl00_MainContent_ListView2_ctrl2_ctl01_')
- have an attributeid
with a value starting withctl00_MainContent_ListView2_ctrl2_ctl01_
-
@class='vehicledetailTable'
- and has an attributeclass
with a value equal tovehicledetailTable
-
contains(@style,'475px;')
- and have an attributestyle
whose value contains475px;
.
Conditions are enclosed in [...]
and linked to or
or and
. They can also be grouped with parentheses. You can also use not(...)
to invert a condition. XPath is very useful in such situations.
source to share