This article mainly introduces the use of the DOMDocument class in PHP to generate HTML instances, including common tag elements, such as forms, tables, and CSS styles. Finally, I wrote a complete example, for more information about how to use core PHP to generate HTML files, see this section.
Recently, when I was querying php.net, I found that the DOMDocument class is very interesting and can be used to generate XML or HTML files, DOMDocument provides us with a series of methods to generate XML/HTML tags and insert them into the DOM. now let's look at how to generate
Here, let's take a look at the effect generated using the method provided by it. for details, see:
1. create a new DOM file
The 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 ();
2. add new HTML elements to the DOM file
The code is as follows:
$ Css_text = 'p {color: # ff00ff ;}';
// Create a new style label and CSS content
$ Style = $ dom-> createElement ('style', $ css_text );
// Add the style label to the DOM file
$ Dom-> appendChild ($ style );
// The output result is as follows:
Here we need to talk about the createElement method. when you want to create
The code is as follows:
$ P_text = 'This is a paragraph .';
// Create a new p tag and content
$ P = $ dom-> createElement ('P', $ p_text );
// Create a new property 'id'
$ Domattriattribute = $ dom-> createAttribute ('id ');
// Add a value for the property 'id'
$ DomAttribute-> value = 'description ';
// Add this property to the p tag
$ P-> appendChild ($ domattrichild );
// Add the p tag to the DOM file
$ Dom-> appendChild ($ p );
// The output result is as follows:
One day
4. Add Form elements
Add textbox
The code is as follows:
$ Input = $ dom-> createElement ('input ');
$ Domattriattribute = $ dom-> createAttribute ('type ');
$ DomAttribute-> value = 'text ';
$ Input-> appendChild ($ domattrichild );
$ Domattriattribute = $ dom-> createAttribute ('name ');
$ DomAttribute-> value = 'e-mail ';
$ Input-> appendChild ($ domattrichild );
$ Dom-> appendChild ($ input );
// The output result is as follows:
5. create a Table
The code is as follows:
$ Table = $ dom-> createElement ('table ');
$ Domattriattribute = $ 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 ($ domattrichild );
$ Dom-> appendChild ($ table );
// The output result is as follows:
Finally, let's get
Complete and complex example:
The 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 a new style label and CSS content
$ Style = $ dom-> createElement ('style', $ css_text );
// Create a new property 'type'
$ Domattriattribute = $ dom-> createAttribute ('type ');
// Add a value for the attribute 'type'
$ DomAttribute-> value = 'text/css ';
// Add this attribute to the style label
$ Style-> appendChild ($ domattrichild );
// Add the style label to the DOM file
$ Dom-> appendChild ($ style );
// Add form
$ Form = $ dom-> createElement ('form ');
$ Dom-> appendChild ($ form );
$ Formattriattribute = $ dom-> createAttribute ('method ');
$ FormAttribute-> value = 'post ';
$ Form-> appendChild ($ formattrichild );
// Add a table
$ Table = $ dom-> createElement ('table ');
$ TableAttribute = $ dom-> createAttribute ('id ');
$ TableAttribute-> value = 'My _ table ';
$ Table-> appendChild ($ tableAttribute );
// Add a new row)
$ Tr = $ dom-> createElement ('tr ');
$ Table-> appendChild ($ tr );
// Add a new 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 row)
$ Tr = $ dom-> createElement ('tr ');
$ Table-> appendChild ($ tr );
// Add a new column)
$ Td = $ dom-> createElement ('TD ', 'first name ');
$ Tr-> appendChild ($ td );
// Add a new column)
$ Td = $ dom-> createElement ('TD ');
$ Tr-> appendChild ($ td );
// Add the input element to the column
$ Input = $ dom-> createElement ('input ');
$ Td-> appendChild ($ input );
$ Tdattriattribute = $ dom-> createAttribute ('type ');
$ TdAttribute-> value = 'text ';
$ Input-> appendChild ($ tdattrichild );
$ Tdattriattribute = $ dom-> createAttribute ('name ');
$ TdAttribute-> value = 'F _ name ';
$ Input-> appendChild ($ tdattrichild );
// Add a new row)
$ Tr = $ dom-> createElement ('tr ');
$ Table-> appendChild ($ tr );
// Add a new column)
$ Td = $ dom-> createElement ('TD ', 'Email ');
$ Tr-> appendChild ($ td );
// Add a new column)
$ Td = $ dom-> createElement ('TD ');
$ Tr-> appendChild ($ td );
// Add the input element to the column
$ Input = $ dom-> createElement ('input ');
$ Td-> appendChild ($ input );
$ Tdattriattribute = $ dom-> createAttribute ('type ');
$ TdAttribute-> value = 'text ';
$ Input-> appendChild ($ tdattrichild );
$ Tdattriattribute = $ dom-> createAttribute ('name ');
$ TdAttribute-> value = 'e-mail ';
$ Input-> appendChild ($ tdattrichild );
// Add a new row)
$ Tr = $ dom-> createElement ('tr ');
$ Table-> appendChild ($ tr );
// Add a new column)
$ Td = $ dom-> createElement ('TD ', 'gender ');
$ Tr-> appendChild ($ td );
// Add a new column)
$ Td = $ dom-> createElement ('TD ');
$ Tr-> appendChild ($ td );
// Add the input element to the column
$ Select = $ dom-> createElement ('select ');
$ Td-> appendChild ($ select );
$ Tdattriattribute = $ dom-> createAttribute ('name ');
$ Tdattrider-> value = 'gender ';
$ Select-> appendChild ($ tdattrichild );
// Add options to the Select drop-down box
$ Opt = $ dom-> createElement ('option', 'male ');
$ Domattriattribute = $ dom-> createAttribute ('value ');
$ Domattrie-> value = 'male ';
$ Opt-> appendChild ($ domattrichild );
$ Select-> appendChild ($ opt );
$ Opt = $ dom-> createElement ('option', 'female ');
$ Domattriattribute = $ dom-> createAttribute ('value ');
$ Domattriale-> value = 'female ';
$ Opt-> appendChild ($ domattrichild );
$ Select-> appendChild ($ opt );
// Add a new row)
$ Tr = $ dom-> createElement ('tr ');
$ Table-> appendChild ($ tr );
// Add a new column)
$ Td = $ dom-> createElement ('TD ', 'interest ');
$ Tr-> appendChild ($ td );
// Add a new column)
$ Td = $ dom-> createElement ('TD ');
$ Tr-> appendChild ($ td );
// Add the input element to the column
$ Radio = $ dom-> createElement ('input ');
$ Td-> appendChild ($ radio );
$ Radattriattribute = $ dom-> createAttribute ('type ');
$ RadAttribute-> value = 'Radio ';
$ Radio-> appendChild ($ radattrichild );
$ Radattriattribute = $ dom-> createAttribute ('name ');
$ Radattriest-> value = 'interest ';
$ Radio-> appendChild ($ radattrichild );
$ Radattriattribute = $ dom-> createAttribute ('id ');
$ RadAttribute-> value = 'php ';
$ Radio-> appendChild ($ radattrichild );
$ Label = $ dom-> createElement ('label', 'php ');
$ LabelAttribute = $ dom-> createAttribute ('');
$ LabelAttribute-> value = 'php ';
$ Label-> appendChild ($ labelAttribute );
$ Td-> appendChild ($ label );
$ Radio = $ dom-> createElement ('input ');
$ Td-> appendChild ($ radio );
$ Radattriattribute = $ dom-> createAttribute ('type ');
$ RadAttribute-> value = 'Radio ';
$ Radio-> appendChild ($ radattrichild );
$ Radattriattribute = $ dom-> createAttribute ('name ');
$ Radattriest-> value = 'interest ';
$ Radio-> appendChild ($ radattrichild );
$ Radattriattribute = $ dom-> createAttribute ('id ');
$ RadAttribute-> value = 'jquery ';
$ Radio-> appendChild ($ radattrichild );
$ Label = $ dom-> createElement ('label', 'jquery ');
$ LabelAttribute = $ dom-> createAttribute ('');
$ LabelAttribute-> value = 'jquery ';
$ Label-> appendChild ($ labelAttribute );
$ Td-> appendChild ($ label );
// Add a new row)
$ Tr = $ dom-> createElement ('tr ');
$ Table-> appendChild ($ tr );
// Add a new column)
$ Td = $ dom-> createElement ('TD ');
$ Tr-> appendChild ($ td );
$ TdAttribute = $ dom-> createAttribute ('colspan ');
$ TdAttribute-> value = '2 ';
$ Td-> appendChild ($ tdattrichild );
// Add the input element to the column
$ Input = $ dom-> createElement ('input ');
$ Td-> appendChild ($ input );
$ Tdattriattribute = $ dom-> createAttribute ('type ');
$ Tdattrimit-> value = 'submit ';
$ Input-> appendChild ($ tdattrichild );
$ Tdattriattribute = $ dom-> createAttribute ('value ');
$ Tdattriup-> value = 'sign-up ';
$ Input-> appendChild ($ tdattrichild );
// Add table to form
$ Form-> appendChild ($ table );
Echo $ dom-> saveHTML ();