简体   繁体   English

PHP中的负查找字符串(正则表达式)

[英]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 我正在尝试寻找在tagname属性中不包含单词(英,英,节)的DIV

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). 请看一下PHP Simple DOM解析器 (或任何其他PHP HTML解析框架)。 Using regular expressions for HTML is something which you should avoid. 为HTML使用正则表达式是您应避免的事情。 The DOM Parser should allow you to iterate over the div sections in your document and access the relevant information. DOM分析器应允许您遍历文档中的div部分并访问相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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