Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How PHP uses DOMDocument classes to generate HTML

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how PHP uses DOMDocument classes to generate HTML". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Effect picture:

First, create a new DOM file

The copy code is as follows:

/ / instantiate the DOMDocument class and specify the version number

$dom = new DOMDocument ('1.0')

/ / output the generated tag or code to the page

Echo $dom- > saveHTML ()

Add a new HTML element to the DOM file

The copy code is as follows:

$css_text ='p {color:#ff00ff;}'

/ / create new style tags and CSS content

$style = $dom- > createElement ('style', $css_text)

/ / add the style tag to the DOM file

$dom- > appendChild ($style)

/ / the following is the output effect

P {color:#ff00ff;}

Here you need to talk about the createElement method. When you want to create a tag and write a Css, you can use the second parameter of the method as the Css content, as shown above. But if you want to create

Tag, the second parameter can be omitted, as follows:

The copy code is as follows:

/ / create a new

Label

$br = $dom- > createElement ('br')

/ / add the

Tag to DOM file

$dom- > appendChild ($br)

Add attributes to the HTML element

The HTML element has a variety of attributes, to which you can add attributes using the createAttribute () method

The copy code is as follows:

$css_text ='p {color:#ff00ff;}'

/ / create new style tags and CSS content

$style = $dom- > createElement ('style', $css_text)

/ / create a new property 'type'

$domAttribute = $dom- > createAttribute ('type')

/ / add a value for the attribute 'type'

$domAttribute- > value = 'text/css'

/ / add this attribute to the style tag

$style- > appendChild ($domAttribute)

/ / add the style tag to the DOM file

$dom- > appendChild ($style)

/ / the following is the output effect

P {color:#ff00ff;}

The copy code is as follows:

$p_text = 'This is a paragraph.'

/ / create new p tags and content

$p = $dom- > createElement ('paired, $p_text)

/ / create a new property 'id'

$domAttribute = $dom- > createAttribute ('id')

/ / add a value for the attribute 'id'

$domAttribute- > value = 'description'

/ / add this attribute to the p tag

$p-> appendChild ($domAttribute)

/ / add the p tag to the DOM file

$dom- > appendChild ($p)

/ / the following is the output effect

One day

Fourth, add Form elements

Add textbox

The copy code is as follows:

$input = $dom- > createElement ('input')

$domAttribute = $dom- > createAttribute ('type')

$domAttribute- > value = 'text'

$input- > appendChild ($domAttribute)

$domAttribute = $dom- > createAttribute ('name')

$domAttribute- > value = 'emurmail'

$input- > appendChild ($domAttribute)

$dom- > appendChild ($input)

/ / the following is the output effect

5. Create Table

The copy code is as follows:

$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)

/ / the following is the output effect

Label

Value

Finally, let's take a complete and complicated example:

The copy code is as follows:

$dom = new DOMDocument ('1.0')

/ / CSS content

$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;}'

/ / create new style tags and CSS content

$style = $dom- > createElement ('style', $css_text)

/ / create a new property 'type'

$domAttribute = $dom- > createAttribute ('type')

/ / add a value for the attribute 'type'

$domAttribute- > value = 'text/css'

/ / add this attribute to the style tag

$style- > appendChild ($domAttribute)

/ / add the style tag to the DOM file

$dom- > appendChild ($style)

/ / add form

$form = $dom- > createElement ('form')

$dom- > appendChild ($form)

$formAttribute = $dom- > createAttribute ('method')

$formAttribute- > value = 'post'

$form- > appendChild ($formAttribute)

/ / add table

$table = $dom- > createElement ('table')

$tableAttribute = $dom- > createAttribute ('id')

$tableAttribute- > value = 'my_table'

$table- > appendChild ($tableAttribute)

/ / add a new line (row)

$tr = $dom- > createElement ('tr')

$table- > appendChild ($tr)

/ / add a new column (column)

$th = $dom- > createElement ('th',' Generate HTML using PHP')

$tr- > appendChild ($th)

$thAttribute = $dom- > createAttribute ('colspan')

$thAttribute- > value ='2'

$th- > appendChild ($thAttribute)

/ / add a new line (row)

$tr = $dom- > createElement ('tr')

$table- > appendChild ($tr)

/ / add a new column (column)

$td = $dom- > createElement ('td',' First Name')

$tr- > appendChild ($td)

/ / add a new column (column)

$td = $dom- > createElement ('td')

$tr- > appendChild ($td)

/ / add input elements to the column (column)

$input = $dom- > createElement ('input')

$td- > appendChild ($input)

$tdAttribute = $dom- > createAttribute ('type')

$tdAttribute- > value = 'text'

$input- > appendChild ($tdAttribute)

$tdAttribute = $dom- > createAttribute ('name')

$tdAttribute- > value = 'fanciname'

$input- > appendChild ($tdAttribute)

/ / add a new line (row)

$tr = $dom- > createElement ('tr')

$table- > appendChild ($tr)

/ / add a new column (column)

$td = $dom- > createElement ('td',' Email')

$tr- > appendChild ($td)

/ / add a new column (column)

$td = $dom- > createElement ('td')

$tr- > appendChild ($td)

/ / add input elements to the column (column)

$input = $dom- > createElement ('input')

$td- > appendChild ($input)

$tdAttribute = $dom- > createAttribute ('type')

$tdAttribute- > value = 'text'

$input- > appendChild ($tdAttribute)

$tdAttribute = $dom- > createAttribute ('name')

$tdAttribute- > value = 'emurmail'

$input- > appendChild ($tdAttribute)

/ / add a new line (row)

$tr = $dom- > createElement ('tr')

$table- > appendChild ($tr)

/ / add a new column (column)

$td = $dom- > createElement ('td',' Gender')

$tr- > appendChild ($td)

/ / add a new column (column)

$td = $dom- > createElement ('td')

$tr- > appendChild ($td)

/ / add input elements to the column (column)

$select = $dom- > createElement ('select')

$td- > appendChild ($select)

$tdAttribute = $dom- > createAttribute ('name')

$tdAttribute- > value = 'gender'

$select- > appendChild ($tdAttribute)

/ / add options for the Select drop-down box

$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)

/ / add a new line (row)

$tr = $dom- > createElement ('tr')

$table- > appendChild ($tr)

/ / add a new column (column)

$td = $dom- > createElement ('td',' Interest')

$tr- > appendChild ($td)

/ / add a new column (column)

$td = $dom- > createElement ('td')

$tr- > appendChild ($td)

/ / add input elements to the column (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)

/ / add a new line (row)

$tr = $dom- > createElement ('tr')

$table- > appendChild ($tr)

/ / add a new column (column)

$td = $dom- > createElement ('td')

$tr- > appendChild ($td)

$tdAttribute = $dom- > createAttribute ('colspan')

$tdAttribute- > value ='2'

$td- > appendChild ($tdAttribute)

/ / add input elements to the column (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)

/ / add table to form

$form- > appendChild ($table)

Echo $dom- > saveHTML ()

This is the end of the introduction to "how PHP uses DOMDocument classes to generate HTML". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report