PHP与preg_match_all匹配

PHP与preg_match_all匹配,php,regex,Php,Regex,我的任务是从HTML中提取数据,我需要为HTML中的每一组p标记获取数据数组。下面是一个HTML示例 <p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 63px; white-space: nowrap;">Title </p> <p class

我的任务是从HTML中提取数据,我需要为HTML中的每一组p标记获取数据数组。下面是一个HTML示例

<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 63px; white-space: nowrap;">Title </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 349px; white-space: nowrap;">1234 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 461px; white-space: nowrap;">$30 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 563px; white-space: nowrap;">$10,000,000 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 777px; white-space: nowrap;">3,000,000 </p>
标题

1234

30美元

10000000美元

3000000

此HTML将重复多次,而“Title”和“1234”标签保持不变,然后在某一点切换到不同的标签。“top”和“left”值将在整个HTML中不断变化。我有能力循环使用现有的“Title”和“1234”标签来匹配这部分内容

$title_label = 'Title';
$number_label = '1234';
preg_match_all('%\d{2}px; white-space: nowrap;">$title_label </p>%', $html_content, $array_match);
$array_cost_name = $array_match[1];
$array_return_name = $array_match[2];
$array_number_name = $array_match[3];
$title_标签='title';
$number_标签='1234';
preg_match_all('%\d{2}px;空白:nowrap;“>$title_label

%”,$html_content,$array_match); $array_cost_name=$array_match[1]; $array_return_name=$array_match[2]; $array_number_name=$array_match[3];
然后,我需要3个数组来包含最后3个标签字段。对于提供的示例HTML,我希望每个数组的第一个值为“$30”、“$10000000”和“3000000”


我不知道如何编写正则表达式来处理这种情况。有人能帮忙吗?

正则表达式不是执行此任务的合适工具,XML解析器要简单得多:

$html = '<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 63px; white-space: nowrap;">Title </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 349px; white-space: nowrap;">1234 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 461px; white-space: nowrap;">$30 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 563px; white-space: nowrap;">$10,000,000 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 777px; white-space: nowrap;">3,000,000 </p>';

$doc = new DOMDocument();
$doc->loadHTML($html);
$xml = simplexml_import_dom($doc);

$parts = $xml->xpath('//p[@class="ft01"]/text()'); // find all texts inside p tags, with class ft01

$array_cost_name = (string) $parts[2];
$array_return_name = (string) $parts[3];
$array_number_name = (string) $parts[4];

echo $array_cost_name ; // $30
echo $array_return_name ; // $10,000,000
echo $array_number_name ; // 3,000,000
$html=”

标题

1234

30美元

10000000美元

3000000”; $doc=新的DOMDocument(); $doc->loadHTML($html); $xml=simplexml\u import\u dom($doc); $parts=$xml->xpath('//p[@class=“ft01”]/text());//使用类ft01查找p标记中的所有文本 $array_cost_name=(字符串)$parts[2]; $array_return_name=(字符串)$parts[3]; $array_number_name=(字符串)$parts[4]; echo$array\u cost\u name;//$30 echo$array\u return\u name;//$10000000 echo$array\u number\u name;//3000000


您可以使用一个简单的全局正则表达式或任何类似的方法来获取您要查找的组,然后删除前2项以仅获取最后3项。下面是一个示例和一个测试链接

$html_content = '<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 63px; white-space: nowrap;">Title </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 349px; white-space: nowrap;">1234 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 461px; white-space: nowrap;">$30 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 563px; white-space: nowrap;">$10,000,000 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 777px; white-space: nowrap;">3,000,000 </p>';

preg_match_all('/ace: nowrap;">(.*) <\/p>/', $html_content, $array_match);

$array_match = array_slice($array_match[0], 2); ;

print_r($array_match);
$html\u content='

标题

1234

30美元

10000000美元

3000000”; preg_match_all('/ace:nowrap;“>(.*)/”,$html_content,$array_match); $array_match=array_slice($array_match[0],2); 打印(数组匹配);


通过正则表达式,您可以尝试以下方法:

\preg_match_all('/<p.*>(.*)<\/p>/', $html, $out);
$result = $out[1];
\preg\u match\u all('/(.*)/',$html,$out);
$result=$out[1];

这将捕获

标记之间的所有字符。

考虑在php中使用Dom解析器,这样会更有效。谢谢,这对我来说非常合适。