0

我试图在预先确定的模式中挑选出所有单词,但它不起作用:

$html = "<tooltip>Do</tooltip> you<tooltip>know</tooltip>";

我希望 preg_match_all 返回

数组([0] => 数组([0] => 做)[1] => 数组([0] => 知道))

使用这种模式:

preg_match_all("/<tooltip ?.*>(.*)<\/tooltip>/", $html, $matches);

相反,它正在返回:

数组([0] => 数组([0] => 你知道吗)[1] => 数组([0] => 知道))

我猜是我的模式错了,但我不知道是什么?>

有任何想法吗?

谢谢

4

4 回答 4

1

这并不完全存在,但用于挑选数据的正则表达式运行良好。只是它构建数组的方式与您正在寻找的内容并不完全匹配。但是通过一些调整,我相信你可以弄清楚

<?php
$html = "<tooltip>Do</tooltip> you<tooltip>know</tooltip>";
preg_match_all("~<tooltip>(.*?)<\/tooltip>~", $html, $matches);
print_r($matches);

foreach($matches[0] as $key => $value) {
    $arr[] = $value;
}

print_r($arr);
?>

$arr 然后返回Array ( [0] => Do [1] => know )更接近您要查找的内容。

于 2013-10-16T09:40:17.273 回答
0

尝试这个:

preg_match_all("/<tooltip>([^<]+)<\/tooltip>/is", $html, $out);

您将获得所需的输出,但在 $out[1] 中而不是在 $out[0] 中。

[1] => Array
    (
        [0] => Do
        [1] => know
    )
于 2013-10-16T09:58:28.120 回答
0

检查这个 SO post为什么我们不使用正则表达式来解析 html。

如果您坚持使用正则表达式提取 html,请使用 @Lee 提供的正则表达式

<tooltip[^>]*>(.*?)</tooltip>

但它会失败(和许多其他人):

<tooltip attr="some > pretend > stuff">Do</tooltip> you<tooltip>know</tooltip>

以上可能永远不会发生在你身上。编程中没有太多保证,但如果有,你不会接受它。DomDocument为您提供了 html 的保证。你的来电

于 2013-10-16T10:06:35.587 回答
0

我不是正则表达式专家,我使用 Expresso 来构建有效的东西,但我不会说它是你可以使用的最好或最强大的正则表达式。

然而,这似乎有效

<tooltip[^>]*>(.*?)</tooltip>

所以:

preg_match_all("/<tooltip[^>]*>(.*?)<\/tooltip>/", $html, $matches);
于 2013-10-16T09:40:24.753 回答