当HTML在1行并且使用<div> </div>完成新的行布局时,保持换行

时间:2011-03-04 09:33:04

标签: php html strip-tags

我需要从网站获取内容

我需要

/html/body/div/div[2]/table/tbody/tr/td/div/div[2]/form/fieldset[2]/table[2]

<table class='properties'>

此处的代码可见:http://paste.pocoo.org/show/347881/

内容包含仅在新行上格式化的所有内容。 我不关心填充和其他格式化,我只想保留新的行。

例如,正确的输出将是

tájékoztató
az eljárás eredményéről
A Közbeszerzések Tanácsa (Szerkesztőbizottsága) tölti ki
A hirdetmény kézhezvételének dátuma____________________
KÉ nyilvántartási szám_________________________________
I. SZAKASZ: AJÁNLATKÉRŐ
I.1) Név, cím és kapcsolattartási pont(ok) 

我遇到的问题是新的行是用div引入的,而不能得到它。

更新

这由PHP cron执行,因此无法访问JS。

2 个答案:

答案 0 :(得分:4)

有一个名为phpQuery的图书馆:http://code.google.com/p/phpquery/

您可以像jQuery一样遍历DOM对象:

phpQuery::newDocument($htmlCode)->find('table.properties');

在mached元素的内容fire strip_tags上,您将获得该表的纯内容。

答案 1 :(得分:2)

诀窍是获取xpath表达式中的内部div,然后使用它们的textContent属性:

<?php

$domd = new DOMDocument();
libxml_use_internal_errors(true);
$domd->loadHTML(file_get_contents("..."));
libxml_use_internal_errors(false);

$domx = new DOMXPath($domd);
$items = $domx->query("/html/body/div/div[2]/table/tr/td/div/div[2]/form/fieldset[2]/table[2]/tr/td/div//div/div[@style='padding-left: 0px;']");

$output = "";
foreach ($items as $item) {
  $output .= $item->textContent . "\n";
}

echo $output;