简体   繁体   中英

How to preg match only if there is this HTML tag?

As shown below in the code, it gets all the matches that have 10 digits and it works great. However I need to filter the matches only with these that are inside this tag

<tr class="main_row pc">

Other numbers are inside this tag (pc is changed to dc)

<tr class="main_row dc">

.

$html= file_get_contents('...');
$matches = array();
preg_match_all('/\d{10}/', $html, $matches);
   foreach ($matches[1] as $match) {
    echo $match.'<br>';
}

How can I filter the numbers?

UPDATE this is the HTML. I want to get the 6914576304 but because there is this tag <tr class="main_row pc">

<tr class="main_row pc">
    <td class="details">
        <a href="./view/4135660/"><span class="p_t">Fiat Cinquecento </span></a>
        <a class="info" href="./view/4135660/">(Info)</a><br>
        <div class="attribs">16345, <span class="p_l">Blue</span>, Phone 6914576304
        </div>
    </td>
</tr>

How about instead of /\\d{10}/ , do:

/<tr class="main_row pc">\d{10}<\/tr>/

You could simplify that too, but I don't know if it's important to you that the number be inside of a tr with both classes specifically or not. Here's some example code and its output:

<?php

$data = '<tr class="main_row pc">0123456789</tr><tr class="main_row pc">1123456789</tr><tr class="main_row dc">2123456789</tr>';

$matches = array();
preg_match_all('/<tr class="main_row pc">\d{10}<\/tr>/', $data, $matches);
print_r($matches);

?>

Output:

Array
(
    [0] => Array
        (
            [0] => <tr class="main_row pc">0123456789</tr>
            [1] => <tr class="main_row pc">1123456789</tr>
        )
)

The first two are matched, the last one is a different class, and is ignored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM