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 . . .

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>');
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.