preg_match_all如何查找仅指定以</p>结尾的<p>标签

时间:2013-01-20 17:21:11

标签: php regex html-parsing

我一直在玩这个,我就是这个......

preg_match_all('/<p>(.*?)<br>(.*?)<p>/s', $offices, $district);

哪个工作正常......但当然有一个记录会导致问题。如何在<p>标记中指定并获取其中包含<br>的所有文字?最好在<br>中指定多一个<p>并排除Eddy Lite标记?

字符串是一个地址,例如:

    <h3>District Offices:</h3>
<p>
317 Dun Avenue<br>Suite 17<br>Port Samson, AK 32675<br>
(XXX) XXX-XXXX<br> 
VOIP: 40800<br> 
FAX (888) xxx-38xx<br> 

</p>

<h4>Staff Assistants:</h4>
<p>Beth Booger and Ly Sweet</p>

<h4>Secretary:</h4>
<p>Eddy Lite </p>

<p>
OK City Hall<br>110 S.E. Five Avenue<br>3rd Floor<br>Corpse, AK 33371<br>
(xxx) 694-xxxx<br> 

</p>

<h4>Staff Assistant:</h4>
<p>Con Sims </p>

    <br />

<h3>Home Office:</h3>

这就是我要回来的:     数组([0] =&gt;数组([0] =&gt;     敦大街317号     17号套房     Port Samson,AK 32675     (XXX)XXX-XXXX

Staff Assistants:

[1] =>

Eddy Lite 

OK City Hall
110 S.E. Five Avenue
3rd Floor
Corpse, AK 33371
(xxx) 694-xxxx
Staff Assistant:

) )

非常感谢任何帮助。       我试过了:       preg_match_all('/

(。*?)

/ s',$ offices,$ district);       preg_match_all('/

(。*)

/ s',$ offices,$ district);       preg_match_all('/

(。?)
(。
?)

/',$ offices,$ district);       preg_match_all( '/

?)
?)
(。*?)

/秒', $ offices,$ district);       preg_match_all('/

(。?)
(。

(。*)

/ s',$ offices ,$ district);

2 个答案:

答案 0 :(得分:0)

试试这个

'~<p>(?=((?!</p>).)*<br>)((?!Eddy Lite).)*?</p>~s'

答案 1 :(得分:0)

一个简单的解决方法是只允许纯文本和<br>标记:

preg_match_all('#<p>([^<>]*<br\s*/?>[^<>]*)+</p>#s', $offices, $district);

通常的说明:这样的正则表达式只适用于连贯和众所周知的输入。