首页 > 作文

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

更新时间:2023-04-06 22:25:16 阅读: 评论:0

在这一章节里, 我们来了解下如何利用核心(core) php 生成 html 文件

最近我在查询 php.net 的时候,发现 domdocument 这个类非常的有意思, 可以用来生成 xml 或 html 文件, domdocument 为我们提供了一系列的方法来生成 xml/html 标签并插入到 dom 中, 现在就让我们来看下如何生成的

这里先来看下, 利用它所提供的方法生成的效果, 见下图:

一、创建新的 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-&g西藏有什么大学t;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)中

$lect = $dom->createelement(‘lect’);

$td->appendchild($lect);

$tdattribute = $dom->createattribute(‘name’);

$tdattribute->value = ‘gender’;

$lect->appendchild($tdattribute);



//为 lect 下拉框添加选项

$opt = $dom->createelement(‘option’, ‘male’);

$domattribute = $dom->createattribute(‘value’);

$domattribute->value = ‘male&大连大学艺术类#8217;;

$opt->appendchild($domattribute);

$lect->appendchild($opt);



$opt = $dom->createelement(‘option’, ‘female’);

$domattribute = $dom->createattribute(‘value’);

$domattribute->value = ‘female’;

$opt->appendchild($domattribute);

$lect->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 = &#建设银行绑定手机8216;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 到 fo七一党课讲稿rm 中

$form->appendchild($table);



echo $dom->savehtml();

本文发布于:2023-04-06 22:25:14,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/a0ce92ccc29800f60186e23160de9e58.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP使用DOMDocument类生成HTML实例(包含常见标签元素).doc

本文 PDF 下载地址:PHP使用DOMDocument类生成HTML实例(包含常见标签元素).pdf

下一篇:返回列表
标签:标签   代码   属性   元素
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图