嗨,我所有的示例代码如下:
<?php
$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>samplecode</title>
</head>
<body>
<div id="warrper">
<div class="box-title">This title is sample</div>
<div class="box-maim">
<div class="box-element-1">
<ul>
<li>sample 1</li>
<li>sample 2</li>
<li>sample 3</li>
<li>sample 4</li>
<li>sample 5</li>
</ul>
</div>
<div class="box-element-1">
<ul>
<li>sample 1</li>
<li>sample 2</li>
<li>sample 3</li>
<li>sample 4</li>
<li>sample 5</li>
</ul>
</div>
<div class="box-element-1">
<ul>
<li>sample 1</li>
<li>sample 2</li>
<li>sample 3</li>
<li>sample 4</li>
<li>sample 5</li>
</ul>
</div>
</div>
</div>
</body>
</html> ';
preg_match( '/<div class="box-maim">(.*?)<\/div>/si' , $html , $match );
print_r($match);
?>
我从 url 加载 html 后的目标是,只获取所选类的标记部分,例如下面的代码:
<div class="box-element-1">
<ul>
<li>sample 1</li>
<li>sample 2</li>
<li>sample 3</li>
<li>sample 4</li>
<li>sample 5</li>
</ul>
</div>
<div class="box-element-1">
<ul>
<li>sample 1</li>
<li>sample 2</li>
<li>sample 3</li>
<li>sample 4</li>
<li>sample 5</li>
</ul>
</div>
<div class="box-element-1">
<ul>
<li>sample 1</li>
<li>sample 2</li>
<li>sample 3</li>
<li>sample 4</li>
<li>sample 5</li>
</ul>
</div>
但我不知道该部分的正确方法。