我有以下HTML:
<tr valign="top">
<td>Name:</td>
<td>John Doe</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr valign="top">
<td>Address:</td>
<td>71 view st, Fitchburg, MA 01420</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr valign="top">
<td>Phone:</td>
<td>978-345-5391</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr>
<td>Email:</td>
<td>johndoe@cs.com</td>
</tr>
我想在相同的REGEX模式中捕获Name
,Phone
和Email
的值,例如:
$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如何工作,不可能在一个模式中有更多匹配?
答案 0 :(得分:2)
显示多行正则表达式的示例。
1.不是问题的最佳解决方案,而是说明能力。
2.不是最好的正则表达式,但最后一点是s
标志。
<?php
$html = <<<EOL
<tr valign="top">
<td>Name:</td>
<td>John Doe</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr valign="top">
<td>Address:</td>
<td>71 view st, Fitchburg, MA 01420</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr valign="top">
<td>Phone:</td>
<td>978-345-5391</td>
</tr>
<tr><td colspan="2"> </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