简体   繁体   中英

Negative lookup string in PHP ( Regular expression)

I have a string like this

<div tagname="chapter_title" class="CHAP_TTL" aidpstyle="CHAP_TTL">testt</div>
<div tagname="section" id="sec01">
<div tagname="title" class="H1" aidpstyle="H1" id="sec01">
     INTRODUCTION<!--title-->
</div>
<div tagname="para" class="CHAP_BM_FIRST" aidpstyle="CHAP_BM_FIRST">test3
<div tagname="emph" class="ITALIC" aidcstyle="ITALIC">buildings</div>   

I'm trying to find the DIV that doesnot contain word (emph,section) in tagname attribute

I used the below pattern but its not showing the right output

 preg_match_all('/<div tagname="(?!emph)(?!section)(?!footnote)
      (?!note).*"/i',$new_updated_html,$divstarttag);

Any takers ??

I checked your code and its works fine except that you need to replace all the new lines and spaces using following two lines-

$string = preg_replace('/\s\s+/', ' ', $subject);

$data = preg_replace('/\r\n/', "", $string);

Please take a look at PHP Simple DOM Parser (or any other PHP HTML parsing framework). Using regular expressions for HTML is something which you should avoid. The DOM Parser should allow you to iterate over the div sections in your document and access the relevant information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM