首页 后端开发 php教程 simplehtmldom Doc api帮助文档_PHP教程

simplehtmldom Doc api帮助文档_PHP教程

Jul 21, 2016 pm 03:19 PM
api doc helper o reference 帮助 文档

API Reference

Helper functions
object str_get_html ( string $content ) Creates a DOM object from a string.
object file_get_html ( string $filename ) Creates a DOM object from a file or a URL.

DOM methods & properties

stringplaintext Returns the contents extracted from HTML.
voidclear () Clean up memory.
voidload ( string $content ) Load contents from a string.
stringsave ( [string $filename] ) Dumps the internal DOM tree back into a string. If the $filename is set, result string will save to file.
voidload_file ( string $filename ) Load contents from a from a file or a URL.
voidset_callback ( string $function_name ) Set a callback function.
mixedfind ( string $selector [, int $index] ) Find elements by the CSS selector. Returns the Nth element object if index is set, otherwise return an array of object.

Element methods & properties

string[attribute] Read or write element's attribure value.
stringtag Read or write the tag name of element.
stringoutertext Read or write the outer HTML text of element.
stringinnertext Read or write the inner HTML text of element.
stringplaintext Read or write the plain text of element.
mixedfind ( string $selector [, int $index] ) Find children by the CSS selector. Returns the Nth element object if index is set, otherwise, return an array of object.

DOM traversing

mixed$e->children ( [int $index] ) Returns the Nth child object if index is set, otherwise return an array of children.
element$e->parent () Returns the parent of element.
element$e->first_child () Returns the first child of element, or null if not found.
element$e->last_child () Returns the last child of element, or null if not found.
element$e->next_sibling () Returns the next sibling of element, or null if not found.
element$e->prev_sibling () Returns the previous sibling of element, or null if not found.
Camel naming convertions You can also call methods with W3C STANDARD camel naming convertions.


string$e->getAttribute ( $name ) string$e->attribute
void$e->setAttribute ( $name, $value ) void$value = $e->attribute
bool$e->hasAttribute ( $name ) boolisset($e->attribute)
void$e->removeAttribute ( $name ) void$e->attribute = null
element$e->getElementById ( $id ) mixed$e->find ( "#$id", 0 )
mixed$e->getElementsById ( $id [,$index] ) mixed$e->find ( "#$id" [, int $index] )
element$e->getElementByTagName ($name ) mixed$e->find ( $name, 0 )
mixed$e->getElementsByTagName ( $name [, $index] ) mixed$e->find ( $name [, int $index] )
element$e->parentNode () element$e->parent ()
mixed$e->childNodes ( [$index] ) mixed$e->children ( [int $index] )
element$e->firstChild () element$e->first_child ()
element$e->lastChild () element$e->last_child ()
element$e->nextSibling () element$e->next_sibling ()
element$e->previousSibling () element$e->prev_sibling ()





// Create a DOM object from a string
$html = str_get_html('

Hello!');

// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');



// Create a DOM object
$html = new simple_html_dom();

// Load HTML from a string
$html->load('Hello!');

// Load HTML from a URL
$html->load_file('http://www.google.com/');

// Load HTML from a HTML file
$html->load_file('test.htm');


// Find all anchors, returns a array of element objects
$ret = $html->find('a');

// Find (N)thanchor, returns element object or null if not found(zero based)
$ret = $html->find('a', 0);

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

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

// Find all element has attribute id
$ret = $html->find('[id]');


// Find all element which id=foo
$ret = $html->find('#foo');

// Find all element which class=foo
$ret = $html->find('.foo');

// Find all anchors and images
$ret = $html->find('a, img');

// Find all anchors and images with the "title" attribute
$ret = $html->find('a[title], img[title]');



// Find all
  • in

      $es = $html->find('ul li');

      // Find Nested
      tags
      $es = $html->find('div div div');

      // Find all in which class=hello
      $es = $html->find('table.hello td');

      // Find all td tags with attribite align=center in table tags
      $es = $html->find(''table td[align=center]');

      // Find all
    • in

        foreach($html->find('ul') as $ul)
        {
        foreach($ul->find('li') as $li)
        {
        // do something...
        }
        }

        // Find first
      • in first

          $e = $html->find('ul', 0)->find('li', 0);

          Supports these operators in attribute selectors:


          [attribute] Matches elements that have the specified attribute.
          [attribute=value] Matches elements that have the specified attribute with a certain value.
          [attribute!=value] Matches elements that don't have the specified attribute with a certain value.
          [attribute^=value] Matches elements that have the specified attribute and it starts with a certain value.
          [attribute$=value] Matches elements that have the specified attribute and it ends with a certain value.
          [attribute*=value] Matches elements that have the specified attribute and it contains a certain value.

          // Find all text blocks
          $es = $html->find('text');

          // Find all comment () blocks
          $es = $html->find('comment');

          // Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
          $value = $e->href;

          // Set a attribute(If the attribute is non-value attribute (eg. checked, selected...), set it's value as true or false)
          $e->href = 'my link';

          // Remove a attribute, set it's value as null!
          $e->href = null;

          // Determine whether a attribute exist?
          if(isset($e->href))
          echo 'href exist!';

          // Example
          $html = str_get_html("
          foo bar
          ");
          $e = $html->find("div", 0);

          echo $e->tag; // Returns: " div"
          echo $e->outertext; // Returns: "
          foo bar
          "
          echo $e->innertext; // Returns: " foo bar"
          echo $e->plaintext; // Returns: " foo bar"


          $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.

          // Extract contents from HTML
          echo $html->plaintext;

          // Wrap a element
          $e->outertext = '
          ' . $e->outertext . '
          ';

          // Remove a element, set it's outertext as an empty string
          $e->outertext = '';

          // Append a element
          $e->outertext = $e->outertext . '
          foo
          ';

          // Insert a element
          $e->outertext = '
          foo
          ' . $e->outertext;

          // If you are not so familiar with HTML DOM, check this link to learn more...

          // Example
          echo $html->find("#div1", 0)->children(1)->children(1)->children(2)->id;
          // or
          echo $html->getElementById("div1")->childNodes(1)->childNodes(1)->childNodes(2)->getAttribute('id');
          You can also call methods with Camel naming convertions.

          mixed$e->children ( [int $index] ) Returns the Nth child object if index is set, otherwise return an array of children.
          element$e->parent () Returns the parent of element.
          element$e->first_child () Returns the first child of element, or null if not found.
          element$e->last_child () Returns the last child of element, or null if not found.
          element$e->next_sibling () Returns the next sibling of element, or null if not found.
          element$e->prev_sibling () Returns the previous sibling of element, or null if not found.

          // Dumps the internal DOM tree back into string
          $str = $html;

          // Print it!
          echo $html;

          // Dumps the internal DOM tree back into string
          $str = $html->save();

          // Dumps the internal DOM tree back into a file
          $html->save('result.htm');

          // Write a function with parameter "$element"
          function my_callback($element) {
          // Hide all tags
          if ($element->tag=='b')
          $element->outertext = '';
          }

          // Register the callback function with it's function name
          $html->set_callback('my_callback');

          // Callback function will be invoked while dumping
          echo $html;

          www.bkjia.comtruehttp://www.bkjia.com/PHPjc/325206.htmlTechArticleAPI Reference Helper functions object str_get_html ( string $content ) Creates a DOM object from a string. object file_get_html ( string $filename ) Creates a DOM object from a fil...
  • 本站声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    AI Hentai Generator

    AI Hentai Generator

    免费生成ai无尽的。

    热门文章

    R.E.P.O.能量晶体解释及其做什么(黄色晶体)
    2 周前 By 尊渡假赌尊渡假赌尊渡假赌
    仓库:如何复兴队友
    4 周前 By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island冒险:如何获得巨型种子
    4 周前 By 尊渡假赌尊渡假赌尊渡假赌

    热工具

    记事本++7.3.1

    记事本++7.3.1

    好用且免费的代码编辑器

    SublimeText3汉化版

    SublimeText3汉化版

    中文版,非常好用

    禅工作室 13.0.1

    禅工作室 13.0.1

    功能强大的PHP集成开发环境

    Dreamweaver CS6

    Dreamweaver CS6

    视觉化网页开发工具

    SublimeText3 Mac版

    SublimeText3 Mac版

    神级代码编辑软件(SublimeText3)

    Word文档在Windows 11/10上打开时为空白 Word文档在Windows 11/10上打开时为空白 Mar 11, 2024 am 09:34 AM

    当您在Windows11/10计算机上打开Word文档时遇到空白页面的问题,可能需要进行修复以解决此状况。造成这一问题的根源多种多样,其中最普遍的原因之一是文档本身损坏。此外,Office文件的损坏也可能导致类似的情况。因此,本文提供的修复方法可能会对您有所帮助。您可以尝试使用一些工具来修复损坏的Word文档,或者尝试将文档转换为其他格式再重新打开。另外,检查系统中的Office软件是否需要更新也是解决此问题的一种方法。通过这些简单的步骤,您可能能够解决Word文档空白打开的Word文档在Win

    如何在 IDE 中查看 Golang 函数文档? 如何在 IDE 中查看 Golang 函数文档? Apr 18, 2024 pm 03:06 PM

    使用IDE查看Go函数文档:将光标悬停在函数名称上。按下热键(GoLand:Ctrl+Q;VSCode:安装GoExtensionPack后,F1并选择"Go:ShowDocumentation")。

    详解Word文档操作:将两页合并为一页 详解Word文档操作:将两页合并为一页 Mar 26, 2024 am 08:18 AM

    Word文档是我们日常工作和学习中使用频率较高的应用程序之一。在处理文档时,有时会遇到需要将两页内容合并为一页的情况。本文将详细介绍在Word文档中如何将两页合并为一页,帮助读者更高效地处理文档排版。在Word文档中,将两页合并为一页的操作通常用于节省纸张和打印成本,或者为了使文档更加紧凑和整洁。以下是合并两页为一页的具体步骤:第一步:打开需要操作的Word

    WPS文档横版怎么设置 WPS文档横版怎么设置 Mar 20, 2024 pm 02:30 PM

    WPS是我们现在工作与日常生活中应用很广泛的办公软件,大家都会选择WPS来进行文档或者是表格信息的更改,你会发现一般用WPS打开文件都是纵向的,那么如何让文件变成横向版呢?WPS横版怎么设置呢?今天小编就来给大家分享一下,感兴趣的小伙伴可不要错过啊!如下图中的WPS页面为纵向,想设置成横向。点击打开WPS的【页面布局】选项卡。点击页面布局选项卡的【张纸方向】按钮。点击下拉菜单中的【横向】。这样,WPS的页面就变成横向了。另外也可以点击页面布局选项卡中下图所示的小按钮。点击后会打开页面设置对话框,

    如何在word文档中嵌入另一个文档 如何在word文档中嵌入另一个文档 Mar 19, 2024 pm 06:10 PM

    一个写好的文档可能会由多个文档内容组合而成,如果是大篇幅的进行使用,可以把一个文档嵌入到另一个文档里,我们想到的传统方法就是进行粘贴,如果内容过大,粘贴起来也很麻烦,下面就教大家如何在word文档中嵌入另一个文档里。首先,在电脑上打开Word文档,然后点击“插入”选项卡,在弹出的插入界面中选择“对象”。接着,通过插入一个对象的方式,将另一个Word文件嵌入到当前文档中。操作步骤如下图所示:  第二步进去对象界面之后,默认是新建,点击由文件创建,点击浏览,选择要插入的word文件,如下图所示:  

    全球数字货币交易十大APP推荐(2025货币交易软件排名) 全球数字货币交易十大APP推荐(2025货币交易软件排名) Mar 12, 2025 pm 05:48 PM

    本文推荐全球十大数字货币交易APP,涵盖币安(Binance)、OKX、火币(Huobi Global)、Coinbase、Kraken、Gate.io、KuCoin、Bitfinex、Gemini和Bitstamp。这些平台在交易对数量、交易速度、安全性、合规性、用户体验等方面各有特色,例如币安以其高交易速度和广泛服务闻名,而Coinbase则更适合新手用户。选择适合自己的平台需要综合考虑自身需求和风险承受能力。 了解全球主流数字货币交易平台,助您安全高效进行数字资产交易。

    btc交易app怎么安装注册? btc交易app怎么安装注册? Feb 21, 2025 pm 07:09 PM

    本篇文章将详细介绍如何安装和注册比特币交易应用。比特币交易应用允许用户管理和交易比特币等加密货币。文章逐步指导用户完成安装和注册过程,包括下载应用程序、创建账户、进行身份验证和首次存款。文章的目标是为初学者提供清晰易懂的指南,帮助他们轻松进入比特币交易的世界。

    PHP中的Web Service PHP中的Web Service Mar 27, 2024 am 08:06 AM

    随着互联网和移动设备的普及,WebService(网络服务)已经成为一个必不可少的技术。现在,WebService已经成为一个标准化的通信协议,因此它可以被各种系统完成。而在PHP开发中,WebService也是一个非常重要且常见的技术。本文将探讨PHP中的Web服务,包括基础知识和使用方法。1.什么是Web服务?Web服务的概念,是指在Web上提供

    See all articles