PHP使用DOMDocument類(lèi)生成HTML實(shí)例(包含常見(jiàn)標(biāo)簽元素)
在這一章節(jié)里, 我們來(lái)了解下如何利用核心(core) PHP 生成 HTML 文件
最近我在查詢 php.net 的時(shí)候,發(fā)現(xiàn) DOMDocument 這個(gè)類(lèi)非常的有意思, 可以用來(lái)生成 XML 或 HTML 文件, DOMDocument 為我們提供了一系列的方法來(lái)生成 XML/HTML 標(biāo)簽并插入到 DOM 中, 現(xiàn)在就讓我們來(lái)看下如何生成的
這里先來(lái)看下, 利用它所提供的方法生成的效果, 見(jiàn)下圖:

一、創(chuàng)建新的 DOM 文件
$dom = new DOMDocument('1.0');
//將生成的標(biāo)簽或代碼輸出到頁(yè)面
echo $dom->saveHTML();
二、在 DOM 文件里添加新的 HTML 元素
//創(chuàng)建新的 style 標(biāo)簽和 CSS 內(nèi)容
$style = $dom->createElement('style', $css_text);
//添加該 style 標(biāo)簽到 DOM 文件中
$dom->appendChild($style);
//如下是輸出效果
<style>
p{color:#ff00ff;}
</style>
這里需要說(shuō)下就是 createElement 方法, 當(dāng)你想創(chuàng)建 <style> 標(biāo)簽并寫(xiě)入 Css, 可以利用該方法的第二個(gè)參數(shù)作為 Css 內(nèi)容,如上所示。 但如果你想創(chuàng)建 <br> 標(biāo)簽, 第二個(gè)參數(shù)即可省略, 如下:
//創(chuàng)建新的 <br> 標(biāo)簽
$br = $dom->createElement('br');
//添加該 <br> 標(biāo)簽到 DOM 文件中
$dom->appendChild($br);
三、為 HTML 元素添加屬性
HTML 元素?fù)碛懈鞣N各樣的屬性, 為其添加屬性可以用到 createAttribute() 方法
$css_text = 'p{color:#ff00ff;}';
//創(chuàng)建新的 style 標(biāo)簽和 CSS 內(nèi)容
$style = $dom->createElement('style', $css_text);
//創(chuàng)建新的屬性 'type'
$domAttribute = $dom->createAttribute('type');
//為屬性 'type' 添加值
$domAttribute->value = 'text/css';
//添加該屬性到 style 標(biāo)簽中
$style->appendChild($domAttribute);
//添加該 style 標(biāo)簽到 DOM 文件中
$dom->appendChild($style);
//如下是輸出效果
<style type="text/css">
p{color:#ff00ff;}
</style>
$p_text = 'This is a paragraph.';
//創(chuàng)建新的 p 標(biāo)簽和內(nèi)容
$p = $dom->createElement('p', $p_text);
//創(chuàng)建新的屬性 'id'
$domAttribute = $dom->createAttribute('id');
//為屬性 'id' 添加值
$domAttribute->value = 'description';
//添加該屬性到 p 標(biāo)簽中
$p->appendChild($domAttribute);
//添加該 p 標(biāo)簽到 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>
最后我們來(lái)一個(gè)完整復(fù)雜一點(diǎn)的例子:
$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 標(biāo)簽和 CSS 內(nèi)容
$style = $dom->createElement('style', $css_text);
//創(chuàng)建新的屬性 'type'
$domAttribute = $dom->createAttribute('type');
//為屬性 'type' 添加值
$domAttribute->value = 'text/css';
//添加該屬性到 style 標(biāo)簽中
$style->appendChild($domAttribute);
//添加該 style 標(biāo)簽到 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 下拉框添加選項(xiàng)
$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找不到的問(wèn)題
- PHP中使用DOMDocument來(lái)處理HTML、XML文檔的示例
- PHP讀取XML文件的方法實(shí)例總結(jié)【DOMDocument及simplexml方法】
- PHP創(chuàng)建XML的方法示例【基于DOMDocument類(lèi)及SimpleXMLElement類(lèi)】
- PHP基于DOMDocument解析和生成xml的方法分析
- PHP 中 DOMDocument保存xml時(shí)中文出現(xiàn)亂碼問(wèn)題的解決方案
- php中DOMDocument簡(jiǎn)單用法示例代碼(XML創(chuàng)建、添加、刪除、修改)
- PHP XML操作類(lèi)DOMDocument
- php基于DOMDocument操作頁(yè)面元素實(shí)例
相關(guān)文章
GD庫(kù)實(shí)現(xiàn)webp轉(zhuǎn)換jpg的PHP程序
PHP程序來(lái)執(zhí)行webp格式轉(zhuǎn)換成jpg格式有幾種方法:一是安裝imagemagick實(shí)現(xiàn),二是安裝GD庫(kù)實(shí)現(xiàn),可以直接用dwebp命令,本文我們將介紹使用PHP的圖像處理庫(kù)GD,編寫(xiě)一個(gè)簡(jiǎn)單的PHP程序來(lái)完成這個(gè)任務(wù)2024-03-03
yii插入數(shù)據(jù)庫(kù)防并發(fā)的簡(jiǎn)單代碼
這篇文章主要介紹了yii插入數(shù)據(jù)庫(kù)防并發(fā)的簡(jiǎn)單代碼,需要的朋友可以參考下2017-05-05
PHP中關(guān)于PDO數(shù)據(jù)訪問(wèn)抽象層的功能操作實(shí)例
下面小編就為大家?guī)?lái)一篇PHP中關(guān)于PDO數(shù)據(jù)訪問(wèn)抽象層的功能操作實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
推薦10個(gè)提供免費(fèi)PHP腳本下載的網(wǎng)站
這篇文章主要介紹了推薦10個(gè)提供免費(fèi)PHP腳本下載的網(wǎng)站,需要的朋友可以參考下2014-12-12
將博客園(cnblogs.com)數(shù)據(jù)導(dǎo)入到wordpress的代碼
博客園限制太多,于是決定從博客園(cnblogs)更換自己個(gè)人的博客。WORDPRESS口碑還不錯(cuò),于是決定用用看。之前發(fā)的數(shù)百篇日志需要導(dǎo)入過(guò)來(lái),在網(wǎng)上搜了一會(huì),發(fā)現(xiàn)沒(méi)有這個(gè)插件,無(wú)奈只能自己寫(xiě)一個(gè)2013-01-01
Laravel基礎(chǔ)-關(guān)于引入公共文件的兩種方式
今天小編就為大家分享一篇Laravel基礎(chǔ)-關(guān)于引入公共文件的兩種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
php實(shí)現(xiàn)mysql封裝類(lèi)示例
這篇文章主要介紹了php實(shí)現(xiàn)mysql封裝類(lèi)示例,需要的朋友可以參考下2014-05-05
Thinkphp框架 表單自動(dòng)驗(yàn)證登錄注冊(cè) ajax自動(dòng)驗(yàn)證登錄注冊(cè)
這篇文章主要介紹了Thinkphp框架 表單自動(dòng)驗(yàn)證登錄注冊(cè) ajax自動(dòng)驗(yàn)證登錄注冊(cè)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
DWZ+ThinkPHP開(kāi)發(fā)時(shí)遇到的問(wèn)題分析
這篇文章主要介紹了DWZ+ThinkPHP開(kāi)發(fā)時(shí)遇到的問(wèn)題,結(jié)合實(shí)例形式分析了DWZ+ThinkPHP在ajax調(diào)用中出現(xiàn)錯(cuò)誤問(wèn)題的解決方法,需要的朋友可以參考下2016-12-12

