當前位置:編程學習大全網 - 網站源碼 - 怎麽讓html識別php代碼?

怎麽讓html識別php代碼?

利用php解析html沒有現成的方法,需要利用第三方插件PHP Simple HTML DOM Parser,它可以以類似jQuery的方式通過css選擇器來返回指定的DOM元素,功能十分強大。

1、首先要在程序的開始引入simple_html_dom.php這個文件

參考代碼:include_once('simple_html_dom.php');

2、PHP Simple HTML DOM Parser提供了3種方式來創建DOM對象

參考代碼如下:

// Create a DOM object from a string

$html = str_get_html('<html><body>Hello!</body></html>');

// Create a DOM object from a URL

$html = file_get_html('');

// Create a DOM object from a HTML file

$html = file_get_html('test.htm');

得到DOM對象後就可以進行各種操作了

// Find all anchors, returns a array of element objects

$ret = $html->find('a');

// Find (N)th anchor, returns element object or null if not found (zero based)

$ret = $html->find('a', 0);

// Find lastest anchor, returns element object or null if not found (zero based)

$ret = $html->find('a', -1);

// Find all <div> with the id attribute

$ret = $html->find('div[id]');

// Find all <div> which attribute id=foo

$ret = $html->find('div[id=foo]');

  • 上一篇:三線扭轉怎麽加指標
  • 下一篇:thinkphp關閉調試模式(APP_DEBUG => false),導致程序出錯,開啟調試模式,不報錯,怎麽解決?
  • copyright 2024編程學習大全網