Non-Greedy Regex <div> tag php -


i trying specific qualifier each instance of part#1amtb00186 html below. need return 4cyl 2.3l - f23a1, balance shaft , 4cyl 2.3l - f23a1, cam. believe regex greedy, cannot figure out how make non-greedy. displays first qualifier of 2.3l l4, engine-f23a1. using:

partno="1amtb00186";  $pattern_short ='{<td\s+class="qualifier"\s*>.*<div>([^<]+)</div>.*' . $partno . '}su'; $matchcount = preg_match_all($pattern_short, $data, $matches); 
<tr> <tr id="61" class="findme"> <td class="productname"> <h3>air , fuel delivery - fuel pumps , related components</h3> <br>electric fuel</td> <td class="qualifier"><div>2.3l l4, engine-f23a1</div></td> <td class="partnum">1amfp00020</td> </tr> <tr id="62" class="odd findme"> <td class="productname"> <h3>air , fuel delivery - fuel pumps , related components</h3> <br>electric fuel</td> <td class="qualifier"><div>3.0l v6, engine-j30a1</div></td> </tr> <tr id="63" class="findme"> <td class="productname"> <h3>belts - timingbelts</h3> <br>timingbelt</td> <td class="qualifier"><div>4cyl 2.3l - f23a1, balance shaft</div></td> <td class="partnum">1amtb00186</td> </tr> <tr id="64" class="odd findme"> <td class="productname"> <h3>belts - timingbelts</h3> <br>timingbelt</td> <td class="qualifier"><div>4cyl 2.3l - f23a1, cam</div></td> <td class="partnum">1amtb00244</td> </tr> </tr> <tr id="63" class="findme"> <td class="productname"> <h3>belts - timingbelts</h3> <br>timingbelt</td> <td class="qualifier"><div>4cyl 2.3l - f23a1, cam</div></td> <td class="partnum">1amtb00186</td> </tr> <tr id="65" class="findme"> <td class="productname"> <h3>belts - timingbelts</h3> <br>timingbelt</td> <td class="qualifier"><div>v6 3.0l - j30a1, cam</div></td> <td class="partnum">1amtb00286</td> </tr> <tr id="66" class="odd findme"> <td class="productname"> <h3>brakes - disc brake pad , hardware kit</h3> <br>front; 7345-d465 ceramic</td> <td class="qualifier"><div>l4 2.3l</div></td> <td class="partnum">1amv300465</td> </tr> 

thank you

in seriousness, please stop trying parse large blocks of html code using regex. it's wrong tool job.

instead, php has got dom parser built in. there's explaination of how use here: how use dom php parser (and plenty of other tutorials around if look).

in short, need this:

libxml_use_internal_errors(true); $dom = new domdocument; $dom->loadhtml($html); $xpath = new domxpath($dom); $query = '//tr/td[@class="partnum" , text() = "1amtb00186"]/preceding-sibling::td[@class="qualifier"]'; foreach ($xpath->query($query) $qualifier) {     echo $qualifier->nodevalue, php_eol; } 

the xpath $query explained:

match td elements class "qualifier" preceding td elements class "partnum" , content "1amtb00186" direct children of tr elements

an alternate variant write xpath be

//tr/td[     @class="qualifier" , following-sibling::td[         @class="partnum" , text() = "1amtb00186"     ] ] 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -