PHP使用DOMDocument類生成HTML實例(包含常見標簽元素)
在這一章節(jié)里, 我們來了解下如何利用核心(core) PHP 生成 HTML 文件
最近我在查詢 php.net 的時候,發(fā)現(xiàn) DOMDocument 這個類非常的有意思, 可以用來生成 XML 或 HTML 文件, DOMDocument 為我們提供了一系列的方法來生成 XML/HTML 標簽并插入到 DOM 中, 現(xiàn)在就讓我們來看下如何生成的
這里先來看下, 利用它所提供的方法生成的效果, 見下圖:
一、創(chuàng)建新的 DOM 文件
$dom = new DOMDocument('1.0');
//將生成的標簽或代碼輸出到頁面
echo $dom->saveHTML();
二、在 DOM 文件里添加新的 HTML 元素
//創(chuàng)建新的 style 標簽和 CSS 內(nèi)容
$style = $dom->createElement('style', $css_text);
//添加該 style 標簽到 DOM 文件中
$dom->appendChild($style);
//如下是輸出效果
<style>
p{color:#ff00ff;}
</style>
這里需要說下就是 createElement 方法, 當你想創(chuàng)建 <style> 標簽并寫入 Css, 可以利用該方法的第二個參數(shù)作為 Css 內(nèi)容,如上所示。 但如果你想創(chuàng)建 <br> 標簽, 第二個參數(shù)即可省略, 如下:
//創(chuàng)建新的 <br> 標簽
$br = $dom->createElement('br');
//添加該 <br> 標簽到 DOM 文件中
$dom->appendChild($br);
三、為 HTML 元素添加屬性
HTML 元素擁有各種各樣的屬性, 為其添加屬性可以用到 createAttribute() 方法
$css_text = 'p{color:#ff00ff;}';
//創(chuàng)建新的 style 標簽和 CSS 內(nèi)容
$style = $dom->createElement('style', $css_text);
//創(chuàng)建新的屬性 'type'
$domAttribute = $dom->createAttribute('type');
//為屬性 'type' 添加值
$domAttribute->value = 'text/css';
//添加該屬性到 style 標簽中
$style->appendChild($domAttribute);
//添加該 style 標簽到 DOM 文件中
$dom->appendChild($style);
//如下是輸出效果
<style type="text/css">
p{color:#ff00ff;}
</style>
$p_text = 'This is a paragraph.';
//創(chuàng)建新的 p 標簽和內(nèi)容
$p = $dom->createElement('p', $p_text);
//創(chuàng)建新的屬性 'id'
$domAttribute = $dom->createAttribute('id');
//為屬性 'id' 添加值
$domAttribute->value = 'description';
//添加該屬性到 p 標簽中
$p->appendChild($domAttribute);
//添加該 p 標簽到 DOM 文件中
$dom->appendChild($p);
//如下是輸出效果
<p id="description">
某一天
</p>
四、添加 Form 元素
添加 textbox
$input = $dom->createElement('input');
$domAttribute = $dom->createAttribute('type');
$domAttribute->value = 'text';
$input->appendChild($domAttribute);
$domAttribute = $dom->createAttribute('name');
$domAttribute->value = 'e-mail';
$input->appendChild($domAttribute);
$dom->appendChild($input);
//如下是輸出效果
<input type="text" name="e-mail">
五、創(chuàng)建 Table
$domAttribute = $dom->createAttribute('id');
$domAttribute->value = 'my_table';
$tr = $dom->createElement('tr');
$table->appendChild($tr);
$td = $dom->createElement('td', 'Label');
$tr->appendChild($td);
$td = $dom->createElement('td', 'Value');
$tr->appendChild($td);
$table->appendChild($domAttribute);
$dom->appendChild($table);
//如下是輸出效果
<table id="my_table">
<tbody>
<tr>
<td>Label</td>
<td>Value</td>
</tr>
</tbody>
</table>
最后我們來一個完整復(fù)雜一點的例子:
$dom = new DOMDocument('1.0');
//CSS 內(nèi)容
$css_text = '';
$css_text .= 'body{width:285px;margin:auto;margin-top:50px;}';
$css_text .= '#my_table{border:1px solid #ececec;}';
$css_text .= '#my_table th{border:1px solid #ececec;padding:5px;text-decoration:underline;}';
$css_text .= '#my_table td{border:1px solid #ececec;padding:5px;}';
$css_text .= '#my_table td:first-child{text-align:right;color:#333333;font-weight:bold;color:#999999;}';
//創(chuàng)建新的 style 標簽和 CSS 內(nèi)容
$style = $dom->createElement('style', $css_text);
//創(chuàng)建新的屬性 'type'
$domAttribute = $dom->createAttribute('type');
//為屬性 'type' 添加值
$domAttribute->value = 'text/css';
//添加該屬性到 style 標簽中
$style->appendChild($domAttribute);
//添加該 style 標簽到 DOM 文件中
$dom->appendChild($style);
//添加 form
$form = $dom->createElement('form');
$dom->appendChild($form);
$formAttribute = $dom->createAttribute('method');
$formAttribute->value = 'post';
$form->appendChild($formAttribute);
//添加 table
$table = $dom->createElement('table');
$tableAttribute = $dom->createAttribute('id');
$tableAttribute->value = 'my_table';
$table->appendChild($tableAttribute);
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
//添加新的列(column)
$th = $dom->createElement('th', 'Generate HTML using PHP');
$tr->appendChild($th);
$thAttribute = $dom->createAttribute('colspan');
$thAttribute->value = '2';
$th->appendChild($thAttribute);
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
//添加新的列(column)
$td = $dom->createElement('td', 'First Name');
$tr->appendChild($td);
//添加新的列(column)
$td = $dom->createElement('td');
$tr->appendChild($td);
//添加 input 元素到列(column)中
$input = $dom->createElement('input');
$td->appendChild($input);
$tdAttribute = $dom->createAttribute('type');
$tdAttribute->value = 'text';
$input->appendChild($tdAttribute);
$tdAttribute = $dom->createAttribute('name');
$tdAttribute->value = 'f_name';
$input->appendChild($tdAttribute);
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
//添加新的列(column)
$td = $dom->createElement('td', 'Email');
$tr->appendChild($td);
//添加新的列(column)
$td = $dom->createElement('td');
$tr->appendChild($td);
//添加 input 元素到列(column)中
$input = $dom->createElement('input');
$td->appendChild($input);
$tdAttribute = $dom->createAttribute('type');
$tdAttribute->value = 'text';
$input->appendChild($tdAttribute);
$tdAttribute = $dom->createAttribute('name');
$tdAttribute->value = 'e-mail';
$input->appendChild($tdAttribute);
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
//添加新的列(column)
$td = $dom->createElement('td', 'Gender');
$tr->appendChild($td);
//添加新的列(column)
$td = $dom->createElement('td');
$tr->appendChild($td);
//添加 input 元素到列(column)中
$select = $dom->createElement('select');
$td->appendChild($select);
$tdAttribute = $dom->createAttribute('name');
$tdAttribute->value = 'gender';
$select->appendChild($tdAttribute);
//為 Select 下拉框添加選項
$opt = $dom->createElement('option', 'Male');
$domAttribute = $dom->createAttribute('value');
$domAttribute->value = 'male';
$opt->appendChild($domAttribute);
$select->appendChild($opt);
$opt = $dom->createElement('option', 'Female');
$domAttribute = $dom->createAttribute('value');
$domAttribute->value = 'female';
$opt->appendChild($domAttribute);
$select->appendChild($opt);
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
//添加新的列(column)
$td = $dom->createElement('td', 'Interest');
$tr->appendChild($td);
//添加新的列(column)
$td = $dom->createElement('td');
$tr->appendChild($td);
//添加 input 元素到列(column)中
$radio = $dom->createElement('input');
$td->appendChild($radio);
$radAttribute = $dom->createAttribute('type');
$radAttribute->value = 'radio';
$radio->appendChild($radAttribute);
$radAttribute = $dom->createAttribute('name');
$radAttribute->value = 'interest';
$radio->appendChild($radAttribute);
$radAttribute = $dom->createAttribute('id');
$radAttribute->value = 'php';
$radio->appendChild($radAttribute);
$label = $dom->createElement('label', 'PHP');
$labelAttribute = $dom->createAttribute('for');
$labelAttribute->value = 'php';
$label->appendChild($labelAttribute);
$td->appendChild($label);
$radio = $dom->createElement('input');
$td->appendChild($radio);
$radAttribute = $dom->createAttribute('type');
$radAttribute->value = 'radio';
$radio->appendChild($radAttribute);
$radAttribute = $dom->createAttribute('name');
$radAttribute->value = 'interest';
$radio->appendChild($radAttribute);
$radAttribute = $dom->createAttribute('id');
$radAttribute->value = 'jquery';
$radio->appendChild($radAttribute);
$label = $dom->createElement('label', 'jQuery');
$labelAttribute = $dom->createAttribute('for');
$labelAttribute->value = 'jquery';
$label->appendChild($labelAttribute);
$td->appendChild($label);
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
//添加新的列(column)
$td = $dom->createElement('td');
$tr->appendChild($td);
$tdAttribute = $dom->createAttribute('colspan');
$tdAttribute->value = '2';
$td->appendChild($tdAttribute);
//添加 input 元素到列(column)中
$input = $dom->createElement('input');
$td->appendChild($input);
$tdAttribute = $dom->createAttribute('type');
$tdAttribute->value = 'submit';
$input->appendChild($tdAttribute);
$tdAttribute = $dom->createAttribute('value');
$tdAttribute->value = 'Sign-Up';
$input->appendChild($tdAttribute);
//添加 table 到 form 中
$form->appendChild($table);
echo $dom->saveHTML();
- 如何解決php domdocument找不到的問題
- PHP中使用DOMDocument來處理HTML、XML文檔的示例
- PHP讀取XML文件的方法實例總結(jié)【DOMDocument及simplexml方法】
- PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】
- PHP基于DOMDocument解析和生成xml的方法分析
- PHP 中 DOMDocument保存xml時中文出現(xiàn)亂碼問題的解決方案
- php中DOMDocument簡單用法示例代碼(XML創(chuàng)建、添加、刪除、修改)
- PHP XML操作類DOMDocument
- php基于DOMDocument操作頁面元素實例
相關(guān)文章
Laravel 中使用 Vue.js 實現(xiàn)基于 Ajax 的表單提交錯誤驗證操作
這篇文章主要介紹了Laravel 中使用 Vue.js 實現(xiàn)基于 Ajax 的表單提交錯誤驗證功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-06-06CodeIgniter安全相關(guān)設(shè)置匯總
這篇文章主要介紹了CodeIgniter安全相關(guān)設(shè)置,需要的朋友可以參考下2014-07-07laravel5.5框架的上傳圖片功能實例分析【僅傳到服務(wù)器端】
這篇文章主要介紹了laravel5.5框架的上傳圖片功能,結(jié)合實例形式分析了laravel5.5框架將圖片上傳到服務(wù)器端的相關(guān)操作技巧與注意事項,需要的朋友可以參考下2019-12-12使用JavaScript創(chuàng)建新樣式表和新樣式規(guī)則
這篇文章主要介紹了使用JavaScript創(chuàng)建新樣式表和新樣式規(guī)則的相關(guān)資料,需要的朋友可以參考下2016-06-06PHP安裝擴展mcrypt以及相關(guān)依賴項深入講解
這篇文章主要介紹了PHP安裝擴展mcrypt以及相關(guān)依賴項深入講解,步驟講解的很清晰,有需要的同學(xué)可以研究下2021-03-03Yii2實現(xiàn)上下聯(lián)動下拉框功能的方法
這篇文章主要介紹了Yii2實現(xiàn)上下聯(lián)動下拉框功能的方法,簡單分析了上下聯(lián)動下拉框功能的概念、原理與實現(xiàn)技巧,需要的朋友可以參考下2016-08-08基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫操作示例
這篇文章主要介紹了基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫操作,結(jié)合實例形式分析了thinkPHP5框架整合QueryList爬取數(shù)據(jù)存入mysql相關(guān)操作技巧及注意事項,需要的朋友可以參考下2019-05-05PHP配置把錯誤日志以郵件方式發(fā)送方法(Windows系統(tǒng))
這篇文章主要介紹了PHP配置把錯誤日志以郵件方式發(fā)送方法(Windows系統(tǒng)),本文給出了配置示例和使用例子,需要的朋友可以參考下2015-06-06