0

我正在尝试获取 html 文本的一部分。我试图让每个单独的列表包含在 h3 标签和底部的图像下。这是示例文本:

任何帮助都会很棒。

谢谢

<h3>Item Summary</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam dictum adipiscing nulla. Aenean id leo non urna sollicitudin lobortis. Sed malesuada diam ut elit accumsan auctor. Proin nisl orci, tempor sed pulvinar ut, semper id nisl. Quisque pellentesque porta facilisis. Duis vestibulum pellentesque commodo. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nulla facilisi. Etiam eget lacus mauris, non accumsan erat. Etiam gravida posuere sollicitudin. Cras id sodales diam. </p>
<h3>Item Features</h3>
<ul>
<li>Feature 1.</li>
<li>Feature 2.</li>
<li>Feature 3.</li>
<li>Feature 4.  </li>
<li>Feature 5.</li>
</ul>
<h3>Item Details</h3>
<ul>
<li>Detail 1</li>
<li>Detail 2</li>
<li>Detail 3</li>
<li>Detail 4</li>
<li>Detail 5</li>
</ul>
<h3>Contact Information</h3>
<ul>
<li>Contact 1</li>
<li>Contact 2</li>
<li>Contact 3</li>
<li>Contact 4</li>

</ul>
<p >
   <img height="100px" src="http://www.mydomain.com/Images/123456.jpg" width="200px"/>
</p>
<p >
   <img height="100px" src="http://www.mydomain.com/Images/123456.jpg" width="200px"/>
</p>
<p >
   <img height="100px" src="http://www.mydomain.com/Images/123456.jpg" width="200px"/>
</p>
<p >
   <img height="100px" src="http://www.mydomain.com/Images/123456.jpg" width="200px"/>
</p>
<p >
   <img height="100px" src="http://www.mydomain.com/Images/123456.jpg" width="200px"/>
</p>

      <img alt="img1" src="000.jpg"/>
4

1 回答 1

5

不要使用正则表达式,使用 DOM 解析器,如 DOMDocument 或 SimpleXMLElement。

$dom = new DOMDocument();
$dom->loadHTML($yourHTML);

$finder = new DOMXPath($dom);

//Get all lists:
$lists = $finder->query('//ul');

// get all lists immediately AFTER h3's
$listsAfterHeader = $finder->query('//h3/following-sibling::ul[position()=1]');
于 2013-02-25T06:07:39.157 回答