Php 保留新行,当HTML在1行上并且新行布局使用<;部门>;

Php 保留新行,当HTML在1行上并且新行布局使用<;部门>;,php,html,strip-tags,Php,Html,Strip Tags,我需要从网站上获取内容 我需要去 /html/body/div/div[2]/table/tbody/tr/td/div/div[2]/form/fieldset[2]/table[2] 或 我面临的问题是,新的线路是与div一起引入的,无法获得它 更新 这将由PHP cron执行,因此无法访问JS。有一个名为phpQuery的库: 您可以像使用jQuery一样遍历DOM对象: phpQuery::newDocument($htmlCode)->find('table.propertie

我需要从网站上获取内容

我需要去

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

我面临的问题是,新的线路是与div一起引入的,无法获得它

更新


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

有一个名为
phpQuery
的库:

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

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

在mached元素的内容上,点燃
strip\u标签
,您将获得该表的纯内容。

有一个名为
phpQuery
的库:

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

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

在mached元素的内容上激发
strip_标记
,您将获得该表的纯内容。

诀窍是获取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;

技巧是获取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;

您不能通过遍历div来生成结果吗?到目前为止,您尝试了什么代码?您使用什么来解析html?遍历div有时可能会导致问题,因为可以嵌套多个级别,并且内容显示在实际布局的同一行上。我刚刚尝试用br-s替换div,但结果是新行太多了。我正在使用regexp获取表源。您不能通过遍历div来生成结果吗?到目前为止,您尝试了什么代码?您使用什么来解析html?遍历div有时可能会导致问题,因为可以嵌套多个级别,并且内容显示在实际布局的同一行上。我刚刚尝试用br-s替换div,但结果是新行太多了。我正在使用regexp获取表源代码。抱歉,我只需要由PHP使用,它将仅由cron脚本执行,无法访问JS。@奔腾10:再次检查,phpQuery是PHP代码,而不是JS-与jQuery类似。抱歉,我只需要由PHP使用,它将仅由cron脚本执行,无法访问JS。@奔腾10:再次检查,phpQuery是php代码,而不是JS——就像jQuery。