Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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
Preg match preg_match_all跳过一个嵌套标记_Preg Match - Fatal编程技术网

Preg match preg_match_all跳过一个嵌套标记

Preg match preg_match_all跳过一个嵌套标记,preg-match,Preg Match,如果您查看此标签: $text = '<div class="inner"> <div class="left"> <h4>text </h4> <p>Abdijstreet 42b<br>2000 city </p> </div> <div class="right"> <span class="red">

如果您查看此标签:

$text = '<div class="inner">
    <div class="left">
        <h4>text </h4>
        <p>Abdijstreet 42b<br>2000 city </p>
    </div>
    <div class="right">
        <span class="red">10:00 - 14:00</span>
    </div>
</div>'
$text='1!'
正文
2000年城市阿布迪街42b

10:00 - 14:00 '
我用它来预赛:

preg_match_all("'<div class=\"inner\">(.*?)</div>'si", $text, $match);  // de ul tags

            $match[1] = array_splice($match[0], 0);

        foreach($match[1] as $val) // hele pagina
        {
                echo $val;
                }
preg_match_all(“(.*si)”,$text,$match);//德乌尔标签
$match[1]=阵列拼接($match[0],0);
foreach($val匹配[1])//hele pagina
{
echo$val;
}

我尝试了很多东西,但我只得到了介于两者之间的东西,而从来没有得到我需要的东西,我做错了什么?

你是在试图得到开头和结尾div标签之间的所有东西吗?如果是这样,那么你真的很接近了。只需从表达式中删除问号
。问号指示脚本在正则表达式中找到下一项后停止匹配。在本例中,下一项是结束div标记。所以一旦找到它,它就停止了。如果您忽略它,它将保持匹配,直到找到最后一个div标记为止

$text = '<div class="inner">
    <div class="left">
        <h4>text </h4>
        <p>Abdijstreet 42b<br>2000 city </p>
    </div>
    <div class="right">
        <span class="red">10:00 - 14:00</span>
    </div>
</div>';

preg_match_all("'<div class=\"inner\">(.*)</div>'si", $text, $match);

print "<pre><font color=red>"; print_r($match); print "</font></pre>";
$text='1!'
正文
2000年城市阿布迪街42b

10:00 - 14:00 '; preg_match_all(“'(*)”si“,$text,$match); 打印“”;打印(匹配);打印“”;

如果你试图取出一个div中的每一个项目,那么你可能想考虑使用DOM而不是正则表达式来解决这个问题。但既然您使用了标记,那么它就在正则表达式中:

Array
(
    [0] => 
            <h4>text </h4>
            <p>Abdijstreet 42b<br>2000 city </p>

    [1] => 
            <span class="red">10:00 - 14:00</span>

)
preg\u match\u all('~\K(.*)(=)
^            ^        ^    ^    ^     ^          ^
1            2        3    4    5     6          7
  • 
    
    Array
    (
        [0] => 
                <h4>text </h4>
                <p>Abdijstreet 42b<br>2000 city </p>
    
        [1] => 
                <span class="red">10:00 - 14:00</span>
    
    )
    
    <div class="   (?!inner)   .*?   >   \K   (.*?)   (?=</div>)
          ^            ^        ^    ^    ^     ^          ^
          1            2        3    4    5     6          7