开发者

Get all text between tags with preg_match_all() or better function?

开发者 https://www.devze.com 2023-01-04 07:53 出处:网络
2010-June-11 <remove>2010-June-2</remove> <remove>2010-June-3</remove> 2010-June-15
2010-June-11
<remove>2010-June-2</remove>
<remove>2010-June-3</remove>
2010-June-15
2010-June-16
2010-June-17
2010-June-3
2010-June-2
2010-June-1

I'm trying to find all instances that are between the <remove> tags

This is what I have:

$pattern = "/<remove>(.*?)<\/remove>/";
preg_match_all($pattern, $_POST['exclude'], $matches);

foreach($matches as $deselect){
    foreach ($deselect as $display){
        echo $display."<br />";
    }
}

This is what it returns:

2010-June-2
2010-June-3
2010-June-2
2010-June-3

Why is it doubling up, and how开发者_开发技巧 do I prevent that?


Don't use regex to parse xml/html...

With that said, the problem is because the match structure looks like:

array(
    0 => array('whole match1', 'whole match 2', 'whole match 3'),
    1 => array('subpattern match 1', 'subpattern match 2', 'subpattern match 3'),
);

So instead of doing your foreach, do a:

if (!empty($matches)) { 
    foreach ($matches[1] as $value) {
        echo $value;
    }
}

or use the PREG_SET_ORDER flag to preg_match_all, which will result in an array structure like:

array( 
    0 => array('whole match1', 'subpattern match 1'),
    0 => array('whole match2', 'subpattern match 2'),
    0 => array('whole match3', 'subpattern match 3'),
);

So then your foreach would become:

if (!empty($matches)) { 
    foreach ($matches as $match) {
        echo $match[1];
    }
}


try using $matches[0] instead of just $matches.

I was having the same issue


Not a regex solution but you may remove duplicated like this:

array_unique($matches);
0

精彩评论

暂无评论...
验证码 换一张
取 消