BloodyMind Posted November 18, 2010 Share Posted November 18, 2010 Here is my the expression I'm trying which is logical, but it fails #<div[^>]*>(?!(<div))</div># any help will be really appreciated Link to comment https://forums.phpfreaks.com/topic/219099-capturing-a-div-that-doesnt-contain-any-divs/ Share on other sites More sharing options...
BloodyMind Posted November 18, 2010 Author Share Posted November 18, 2010 using PCRE since POSIX are deprecated Link to comment https://forums.phpfreaks.com/topic/219099-capturing-a-div-that-doesnt-contain-any-divs/#findComment-1136291 Share on other sites More sharing options...
.josh Posted November 19, 2010 Share Posted November 19, 2010 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. Link to comment https://forums.phpfreaks.com/topic/219099-capturing-a-div-that-doesnt-contain-any-divs/#findComment-1136400 Share on other sites More sharing options...
shehabic Posted November 19, 2010 Share Posted November 19, 2010 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 https://forums.phpfreaks.com/topic/219099-capturing-a-div-that-doesnt-contain-any-divs/#findComment-1136732 Share on other sites More sharing options...
sasa Posted November 19, 2010 Share Posted November 19, 2010 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 https://forums.phpfreaks.com/topic/219099-capturing-a-div-that-doesnt-contain-any-divs/#findComment-1136769 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.