Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 预匹配所有不正确的图案_Php_Preg Match All - Fatal编程技术网

Php 预匹配所有不正确的图案

Php 预匹配所有不正确的图案,php,preg-match-all,Php,Preg Match All,我试图在预先确定的模式中挑出所有单词,但它不起作用: $html = "<tooltip>Do</tooltip> you<tooltip>know</tooltip>"; $html=“你知道吗”; 我想让preg_match_所有人都回来 数组([0]=>Array([0]=>Do)[1]=>Array([0]=>know)) 使用此模式: preg_match_all("/<tooltip ?.*>(.*)<\/tool

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

$html = "<tooltip>Do</tooltip> you<tooltip>know</tooltip>";
$html=“你知道吗”;
我想让preg_match_所有人都回来

数组([0]=>Array([0]=>Do)[1]=>Array([0]=>know))

使用此模式:

preg_match_all("/<tooltip ?.*>(.*)<\/tooltip>/", $html, $matches);
preg_match_all(“/(.*)/”,$html,$matches);
相反,它正在返回:

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

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

有什么想法吗


谢谢

这还不太清楚,但是用于提取数据的正则表达式工作得很好。只是它构建阵列的方式与您所寻找的并不完全匹配。但只要稍加调整,我相信你会明白的

<?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然后返回
数组([0]=>Do[1]=>know)
,它更接近您所要查找的内容。

我不是正则表达式专家,我使用Expresso来构建一些有用的东西,但我不会说它是您可以使用的最好或最健壮的正则表达式

然而,这似乎有效

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

preg_match_all(“/]*>(.*?/”,$html,$matches);
试试这个:

preg_match_all("/<tooltip>([^<]+)<\/tooltip>/is", $html, $out);
检查这个,为什么我们不使用正则表达式来解析html

如果您坚持使用regex提取html,那么请使用提供的regex@Lee

<tooltip[^>]*>(.*?)</tooltip>
]*>(**?)
但它将失败(以及许多其他原因):

你知道吗

上述情况可能永远不会发生在你身上。编程中没有太多的保证,但如果有,你不会接受。用html为您提供了保证。您的调用

什么是:(.*)实际上是做什么的?一个点匹配除换行符、星号标记重复之外的每个字符,问号是一个量词,它将前面的标记标记为可选标记。
[1] => Array
    (
        [0] => Do
        [1] => know
    )
<tooltip[^>]*>(.*?)</tooltip>
<tooltip attr="some > pretend > stuff">Do</tooltip> you<tooltip>know</tooltip>