欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP使用DOMDocument类生成HTML实例(包含常见标签元素)

程序员文章站 2023-11-11 23:45:46
在这一章节里, 我们来了解下如何利用核心(core) php 生成 html 文件   最近我在查询 php.net 的时候,发现 domdocument 这...

在这一章节里, 我们来了解下如何利用核心(core) php 生成 html 文件
 
最近我在查询 php.net 的时候,发现 domdocument 这个类非常的有意思, 可以用来生成 xml 或 html 文件, domdocument 为我们提供了一系列的方法来生成 xml/html 标签并插入到 dom 中, 现在就让我们来看下如何生成的
 
这里先来看下, 利用它所提供的方法生成的效果, 见下图:

PHP使用DOMDocument类生成HTML实例(包含常见标签元素)

一、创建新的 dom 文件

复制代码 代码如下:
//实例化 domdocument 类,并指定版本号
$dom = new domdocument('1.0');
 
//将生成的标签或代码输出到页面
echo $dom->savehtml();

二、在 dom 文件里添加新的 html 元素
复制代码 代码如下:
$css_text = 'p{color:#ff00ff;}';
 
//创建新的 style 标签和 css 内容
$style = $dom->createelement('style', $css_text);
 
//添加该 style 标签到 dom 文件中
$dom->appendchild($style);
 
//如下是输出效果
<style>
    p{color:#ff00ff;}
</style>

这里需要说下就是 createelement 方法, 当你想创建 <style> 标签并写入 css, 可以利用该方法的第二个参数作为 css 内容,如上所示。 但如果你想创建 <br> 标签, 第二个参数即可省略, 如下:
复制代码 代码如下:

//创建新的 <br> 标签
$br = $dom->createelement('br');
 
//添加该 <br> 标签到 dom 文件中
$dom->appendchild($br);

三、为 html 元素添加属性
 
html 元素拥有各种各样的属性, 为其添加属性可以用到 createattribute() 方法
复制代码 代码如下:

$css_text = 'p{color:#ff00ff;}';
 
//创建新的 style 标签和 css 内容
$style = $dom->createelement('style', $css_text);
 
//创建新的属性 '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.';
 
//创建新的 p 标签和内容
$p = $dom->createelement('p', $p_text);
 
//创建新的属性 '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">

五、创建 table
复制代码 代码如下:
$table = $dom->createelement('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>

最后我们来一个完整复杂一点的例子:
复制代码 代码如下:

$dom = new domdocument('1.0');
 
//css 内容
$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;}';
 
//创建新的 style 标签和 css 内容
$style = $dom->createelement('style', $css_text);
 
//创建新的属性 '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();