Jump to content

[SOLVED] preg_match_all returns only one match


propellerhead

Recommended Posts

Hi.

 

I'm new with regular expressions so this may not be a big deal for you, but I'm stuck for hours on this bit and I can't find the solution. Here I go:

 

I have a string of this kind

 

<custom>
    probe1
    <custom>
        probe2
    </custom>
    probe3
</custom>
<custom>
    probe4
</custom>
<custom>
    probe5
</custom>

 

I'm trying to match only those custom tags that don't have child (custom) tags in them, so I use this regex

 

/<custom>((?!.*<custom>).*)<\/custom>/s

 

and so, it looks fine on first sight, but there is a problem. When I make the match using preg_match_all it onlly returns the last matching pattern

 

<custom>
    probe5
</custom>

 

here is the array dump using print_r

 

Array
(
    [0] => Array
        (
            [0] => <custom>
    probe5
</custom>
            [1] => 
    probe5

        )

)

 

How do I fix this? I know that there must be something wrong with the regex, but my knowledge in this field is limited and I can't find the solution.

 

Here is the whole code

 

<?php
$string = "
<custom>
    probe1
    <custom>
        probe2
    </custom>
    probe3
</custom>
<custom>
    probe4
</custom>
<custom>
    probe5
</custom>";

$regex = "/<custom>((?!.*<custom>).*)<\/custom>/s";

preg_match_all($regex, $string, $arry, PREG_SET_ORDER);
print_r($arry);

?>

 

I'd be very very thankful :)

Link to comment
Share on other sites

try

<?php
$string = "
<custom>
    probe1
    <custom>
        probe2
    </custom>
    probe3
</custom>
<custom>
    probe4
</custom>
<custom>
    probe5
</custom>";

//$regex = "/<custom>((?!.*<custom>).*)<\/custom>/s";

//preg_match_all($regex, $string, $arry, PREG_SET_ORDER);
$a = explode('<custom>',$string);
foreach ($a as $b){
$b = explode('</custom>',$b);
if (count($b) > 1) $arry[] = $b[0];
}
print_r($arry);

?>f/code]

Link to comment
Share on other sites

Regular expressions are not the best tool for nested data, but this works:

 

<pre>
<?php

$data = <<<DATA
<custom>
    probe1
    <custom>
        probe2
    </custom>
    probe3
</custom>
<custom>
    probe4
</custom>
<custom>
    probe5
</custom>
DATA;

preg_match_all('%<custom>(??!<custom>)(?!</custom>).)*<\/custom>%s', $data, $matches);
print_r($matches);

?>
</pre>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.