简体   繁体   中英

Regular expression - PHP Preg Match

I am learning to use Regular expressions and would like to grab some data from a table:

The file looks like this:

$subject = 
<tbody>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
            </tr>
            <tr>
                <td>4</td>
                <td>5</td>
                <td>6</td>
            </tr>
        </tbody>

Currently I am doing the following:

$pattern = "/<tr>.*?<td><\/td>.*?<td>(.*?)<\/td>.../s";

preg_match( $pattern, $subject, $result);

This will output an array:

$result = [
    0 => "tbody>...",
    1 => 1,
    2 => 2,
    3 => 3,
    4 => 4 ... n     
]

This seems inefficient so I am attempting to grab a repeated pattern like so:

$pattern = "/<td>([0-9]{1,2})<\/td>/s";

This however only grabs the first number: 1

What would be the best way to go about this?

You should use preg_match_all instead of preg_match to perform the search on the entire var

http://php.net/manual/en/function.preg-match-all.php

if (preg_match_all( $pattern, $subject, $matches)) {
    var_dump($matches);
}

Here's a way to accomplish this using a parser:

$subject = '
<tbody>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
            </tr>
            <tr>
                <td>4</td>
                <td>5</td>
                <td>6</td>
            </tr>
        </tbody>';
$html = new DOMDocument();
$html->loadHTML($subject);
$tds = $html->getElementsByTagName('td');
foreach($tds as $td){
    echo $td->nodeValue . "\n";
    if(is_numeric($td->nodeValue)) {
        echo "it's a number \n"; 
    }
}

Output:

1
it's a number 
2
it's a number 
3
it's a number 
4
it's a number 
5
it's a number 
6
it's a number 

To get all the values and not stopping after the first match you need to use the g flag.

In php this is implemented in the preg_match_all function.

Since the data will always be contained in a td you can do the following:

preg_match_all("/<td>(.*)<\/td>", $subject, $matches);
var_dump($matches);

Where the $subject contains you html and you should see an array of all your table data.

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