English 中文(简体)
++ Xpath 带内装的中文本
原标题:php Xpath getting innerHTML with innerHTML tags

我有超文本文档格式,如:

<p class="p1">subject</p>
<p class="p2">detail <span>important</span></p>

<p class="p1">subject</p>
<p class="p2">detail<span>important</span></p>

我写了一份PHP代码,以自动取出每页1页,并详细将其插入我的我的图表。

这是我的法典:

$doc = new DOMDocument();

$doc->loadHTMLFile("file.html");

$xpath = new DomXpath($doc);

$subject = $xpath->query( //p );


for ($i = 0 ; $i < $subject->length-1 ; $i ++) {

if ($subject->item($i)->getAttribute("class") == "p1")
    echo $subject->item($i)->nodeValue;
}
...

这不是我的全面法典,而是:

echo $subject->item($i)->nodeValue;

给我<代码><p>detail important</p>,没有<span></span> tag。

围绕细节中的“进口”部分进行标记非常重要。 是否有任何职能可以做到,而不必走头?

预 收

问题回答

我对我的回答是: 借助于单纯的超文本处理

foreach($html->find( p ) as $element) {

 switch ($element->class) {
      case  p1 :
                     $subject = $element;
                     break;
      case  p2 : $detail .= html_entity_decode($element);

 }

}

the trick is in:

html_entity_decode($element);

每当我需要打下超文本时,我就通过简单的超文本处理:

http://simplehtmldom.sourceforge.net/

我建议使用1.11版本。 由于各种原因,有1.5个被打断。

旧问题,但有一个一线。 执行部分第3段:

$subject =$xpath->query (/p/*);

之后:

echo$doc->saveHtml($subject->item($i));

随附<条码>*,请在内部查询(不含包装段标签);否则,你将带上封段。

实例:

$html =  <div><p>ciao questa è una <b>prova</b>.</p></div> ;
$dom = new DomDocument($html);
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$node = $xpath->query( .//div/* ); // with * you get inner html without surrounding div tag; without * you get inner html with surrounding div tag
$innerHtml = $dom->saveHtml($node);
var_dump($innerHtml);

产出:<p>ciaosearcha è una <b>prova</b></p>





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签