PHP uses the DOMDocument class to generate HTML instances (including common tag elements)

Source: Internet
Author: User

This section describes how to use core PHP to generate HTML files.
 
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
Copy codeThe 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
Copy codeThe 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:
<Style>
P {color: # ff00ff ;}
</Style>
The createElement method is used to create the <style> label and write the Css. The second parameter of this method can be used as the Css content, as shown above. However, if you want to create a <br> tag, the second parameter can be omitted, as shown below:
Copy codeThe Code is as follows:
// Create a new <br> tag
$ Br = $ dom-> createElement ('br ');
 
// Add the <br> tag to the DOM File
$ Dom-> appendChild ($ br );
3. Add attributes for HTML elements
 
HTML elements have various attributes. You can use the createAttribute () method to add attributes to them.
Copy codeThe Code is as follows:
$ Css_text = 'P {color: # ff00ff ;}';
 
// 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 );
 
// The output result is as follows:
<Style type = "text/css">
P {color: # ff00ff ;}
</Style>
Copy codeThe 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:
<P id = "description">
One day
</P>
4. Add Form elements
 
Add textbox
Copy codeThe 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:
<Input type = "text" name = "e-mail">
5. Create a Table
Copy codeThe 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:
<Table id = "my_table">
<Tbody>
<Tr>
<Td> Label </td>
<Td> Value </td>
</Tr>
</Tbody>
</Table>
Finally, let's getComplete and complex example:
Copy codeThe 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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.