PHP返回多个匹配相同的REGEX模式

时间:2016-11-20 17:11:27

标签: php regex

我有以下HTML:

<tr valign="top">
        <td>Name:</td>
        <td>John Doe</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr valign="top">
        <td>Address:</td>
        <td>71 view st, Fitchburg, MA 01420</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr valign="top">
        <td>Phone:</td>
        <td>978-345-5391</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
        <td>Email:</td>
        <td>johndoe@cs.com</td>
</tr>

我想在相同的REGEX模式中捕获NamePhoneEmail的值,例如:

$pattern = "/Name:<\/td>\s*<td>(.*)<\/td>.*Address:<\/td>\s*<td>(.*)<\/td>.*card ([0-9].*)<\/td>/m";

如果我单独尝试,例如:

$pattern = "/Name:<\/td>\s*<td>(.*)<\/td>/m";
$pattern = "/Phone:<\/td>\s*<td>(.*)<\/td>/m";
$pattern = "/Email:<\/td>\s*<td>(.*)<\/td>/m";

没关系。也许我不明白REGEX如何工作,不可能在一个模式中有更多匹配?

1 个答案:

答案 0 :(得分:2)

显示多行正则表达式的示例。 1.不是问题的最佳解决方案,而是说明能力。 2.不是最好的正则表达式,但最后一点是s标志。

<?php
$html = <<<EOL
<tr valign="top">
        <td>Name:</td>
        <td>John Doe</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr valign="top">
        <td>Address:</td>
        <td>71 view st, Fitchburg, MA 01420</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr valign="top">
        <td>Phone:</td>
        <td>978-345-5391</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
        <td>Email:</td>
        <td>johndoe@cs.com</td>
</tr>
EOL;

$pattern = "#Name:</td>.*<td>(.*?)</td>.*Address:</td>.*<td>(.*?)</td>.*Phone:</td>.*<td>(.*?)</td>.*Email:</td>.*<td>(.*?)</td>#s";

if(preg_match($pattern, $html, $matches)) {
    printf("Name: %s\n", $matches[1]);
    printf("Address: %s\n", $matches[2]);
    printf("Phone: %s\n", $matches[3]);
    printf("Email: %s\n", $matches[4]);
}


?>

产量

Name: John Doe
Address: 71 view st, Fitchburg, MA 01420
Phone: 978-345-5391
Email: johndoe@cs.com