Jump to content

capturing a div that doesn't contain any divs


BloodyMind

Recommended Posts

So you have a negative lookahead looking for the absence of <div you aren't actually consuming any characters, matching anything in-between your main opening div and closing div.

 

So what would the correct regex be for such a case , I have a very simillar case . . .

Link to comment
Share on other sites

try

<?php
function my_find($data, $start_tag, $end_tag){
    $end = strpos($data, $end_tag);
    if ($end){
        if (($start = strpos($data, $start_tag)) !== false){
            while ((($tmp = strpos($data, $start_tag, $start+1 )) !== false) and $tmp < $end)
                    $start = $tmp;
        } else echo 'no open tag';
        return substr($data, $start, $end - $start + strlen($end_tag));
    }
}

$test = '<div>1 <div>2 <div>3<div>4</div></div></div></div>';
echo my_find($test, '<div', '</div>');
?>

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.