我正在尝试在下面的$ string中捕获文本“Capture This”。
$string = "</th><td>Capture This</td>";
$pattern = "/<\/th>\r.*<td>(.*)<\/td>$/";
preg_match ($pattern, $string, $matches);
echo($matches);
然而,这只是返回“数组”。我也尝试使用print_r打印$ match,但这给了我“Array()”。
此模式只出现一次,所以我只需要匹配一次。有人可以告诉我我做错了吗?
答案 0 :(得分:2)
问题是您需要CR字符\r
。此外,您应该在捕获组内部进行搜索延迟,并使用print_r
输出数组。像这样:
$pattern = "/<\/th>.*<td>(.*?)<\/td>$/";
您可以在此处看到它:http://codepad.viper-7.com/djRJ0e
请注意,建议使用正确的html解析器而不是使用正则表达式来解析html。
答案 1 :(得分:1)
两件事:
您需要从正则表达式中删除\r
,因为输入字符串中没有回车符。
将echo($matches)
更改为print_r($matches)
或var_dump($matches)