Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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/5/ruby-on-rails-4/2.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_Regex - Fatal编程技术网

Php 在单个正则表达式中匹配多个引用

Php 在单个正则表达式中匹配多个引用,php,regex,Php,Regex,我有一根看起来像 $html = <<<EOT <p><b>There are currently five entries in the London Borough of Barking &amp; Dagenham (LBBD):</b></p> <p>My string 1<br> My another string<br> And this is also my string&

我有一根看起来像

$html = <<<EOT
<p><b>There are currently five entries in the London Borough of Barking &amp; Dagenham (LBBD):</b></p>
<p>My string 1<br>
My another string<br>
And this is also my string<br></p>
<p><i>Some text over here</i></p>
EOT;
我正在尝试提取我的字符串1,我的另一个字符串,这也是我使用php preg_match的字符串 到目前为止,我得到的是

preg_match("/There are currently .+ entries in .+:<\/b><\/p>\n<p>(.+<br>)\n+/", $html, $matches);
print_r($matches);
但它只返回原始字符串和第一次出现的字符串。是否有方法返回字符串中出现的匹配数组?谢谢使用。PHP不像大多数语言那样,在全局匹配或替换时使用g修饰符。相反,您需要使用preg_match vs.preg_match_all,或者在使用时指定一个$limit使其不是全局的

默认情况下,preg_match_all将使用标志preg_模式顺序对$matches数组进行排序。换句话说:$matches[0]将是完整匹配的数组,$matches[1]将是捕获组1的数组。这意味着count$匹配!==$匹配的数量。如果希望$matches[0]成为第一个匹配及其捕获组的数组,请使用标志PREG_SET_ORDER:


是否有方法返回字符串中出现的匹配数组? 是的,功能是

现在,假设您真的只需要文本,而不需要任何html元素,您可以使用这个

preg_match_all("/(<p>)?(.+)<br>/", $html, $matches);
然后,您需要在$matches[2]中查找所需的数组。这是因为所有匹配项都存储在$matches[0]中,第一个分组存储在捕获标记的$matches[1]中,然后您的内容被捕获在$matches[2]第二个分组中。如果有更多的分组,他们会遵循相同的模式


也就是说,您可能应该考虑使用DOM解析器来处理类似的事情,因为正则表达式通常在解析HTML方面非常糟糕。

您需要两个入口点,第一个是当前存在的句子。。。直到开始标记,第二个从标记和换行符之后的最后一个匹配结束时开始

第一个结果将使用第一个入口点,下一个结果将使用第二个入口点

\G是与先前比赛结束时的位置相匹配的锚。这个特性很有趣,因为preg_match_会重试匹配模式,直到字符串结束。但是由于\G是在字符串的开头初始化的,我们需要避免这种情况的出现\A不在字符串的开头


我用[^非常感谢,这似乎是最清楚、最详细的答案
preg_match_all("/(<p>)?(.+)<br>/", $html, $matches);
$pattern = <<<'EOD'
~                    # using this delimiter instead of / avoids to escape all
                     # the slashes

(?:
    # first entry point
    \QThere are currently \E
    [^<]+?
    \Q entries in \E
    [^<]+ </b> </p> \n <p>
  |
    # second entry point
    (?!\A)\G
    <br>\n
)
\K           # removes all that have been matched before from match result
[^<]+        # the string you want
~x
EOD;

if (preg_match_all($pattern, $text, $matches))
    var_dump($matches[0]);