【问题标题】:PHP simple DOM parser confusion, help echo the tags: <div>content code</div>PHP简单DOM解析器混淆,帮助回显标签:<div>内容代码</div>
【发布时间】:2011-05-11 19:49:34
【问题描述】:

我对我正在处理的内容感到困惑。我正在尝试使用简单的 HTMl DOM 解析器来回显 &lt;div&gt;&lt;/div&gt; 的每个打开到关闭标签。使用来自:(http://simplehtmldom.sourceforge.net/manual.htm) 的示例,将尝试在主要 div 标记内回显文本区域。

// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]');

// Find all <div> with the id attribute
$ret = $html->find('div[id]');

如果我想回显包含 &lt;div&gt; 及其 id、文本或其中的任何代码,并回显关闭标签 &lt;/div&gt;,我将确定代码的结束位置。

【问题讨论】:

    标签: php dom


    【解决方案1】:

    documentation for simplehtmldom 中的“如何访问 HTML 元素的属性?”在“魔法属性”选项卡上:

    // Example
    $html = str_get_html("<div>foo <b>bar</b></div>");
    $e = $html->find("div", 0);
    
    echo $e->tag; // Returns: " div"
    echo $e->outertext; // Returns: " <div>foo <b>bar</b></div>"
    echo $e->innertext; // Returns: " foo <b>bar</b>"
    echo $e->plaintext; // Returns: " foo bar"
    
    Attribute Name  Usage
    $e->tag            Read or write the tag name of element.
    $e->outertext      Read or write the outer HTML text of element.
    $e->innertext      Read or write the inner HTML text of element.
    $e->plaintext      Read or write the plain text of element.
    

    【讨论】:

    • 完成了对问题的编辑,为了澄清它,我想回显打开的
      标记、其内容/s 以及关闭的
      标记。
    • @Stupefy outertext 怎么不这样做?
    • @Stupefy 很好,因为它有效并且这是唯一提供的答案,因此鼓励您通过勾选答案旁边的复选标记大纲(就在投票控件下方)将此答案标记为已接受的答案.谢谢。
    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 2019-12-24
    • 2013-01-06
    • 1970-01-01
    相关资源
    最近更新 更多