简体   繁体   English

正则表达式-PHP Preg Match

[英]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); 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 但是,这仅获取第一个数字: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 您应该使用preg_match_all而不是preg_match对整个变量执行搜索。

http://php.net/manual/en/function.preg-match-all.php http://php.net/manual/zh/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. 要获得所有值并且在第一个匹配项后不停止,您需要使用g标志。

In php this is implemented in the preg_match_all function. 在php中,这是在preg_match_all函数中实现的。

Since the data will always be contained in a td you can do the following: 由于数据将始终包含在td中,因此您可以执行以下操作:

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. $ subject包含html的位置,您应该看到所有表数据的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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