Home > Backend Development > PHP Tutorial > 这种正则表达式怎么写?求高手解答!

这种正则表达式怎么写?求高手解答!

WBOY
Release: 2016-06-23 14:39:40
Original
1052 people have browsed it

有一个网页中的代码如下:

<span class="1">      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div></span><div class="2">      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div></div><div class="3">      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div></div><span class="4">      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div></span>...
Copy after login

大家注意到没有 class="1",class="2",class="3",...是有规律的,但是前面的标签没有规律,有时是span,有时是div,我想要的是用php如何写正则表达式获取class="1",class="2",class="3",...的内容,正则表达式这方面学的不好,试了半天都写不出来,求高手解答!


回复讨论(解决方案)

html或xml的东西有专门的dom api,特别是标签嵌套的html,尽量不要尝试用正则去获取,尤其是php的正则,这里涉及到正则的递归,即使php能有象其它语言提供的正则平衡组,也最好不要用。

<(span|div)\s+class=\"\d\">\s+(.*?)<\/h3>\s+\s*(.*?)\s*<\/div>\s*<\/(span|div)>

谢谢两位的回答!

$s =<<< TXT<span class="1">      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div></span><div class="2">      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div></div><div class="3">      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div></div><span class="4">      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div></span>TXT;
Copy after login

方案1
include 'phpquery.php';$doc = phpQuery::newDocument($s);echo $doc->find('.1')->html();echo pq('.2')->html();
Copy after login
      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div>      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div>
Copy after login
方案2
include 'html_document.php';$p = new html_document( $s, 0);foreach($p->find('.\d') as $v) {    echo "$v->innerHTML\n";}
Copy after login
      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div>      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div>      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div>      <h3 class="title">testtesttesttesttesttestt</h3>      <div class="con">            <span>testtesttest</span><p>testtesttesttesttesttesttesttesttest</p>      </div>
Copy after login

感谢版主大大 ,手快早给结贴了

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template