PHP使用DOMDocument類產生HTML執行個體(包含常見標籤元素)_PHP教程

來源:互聯網
上載者:User
在這一章節裡, 我們來瞭解下如何利用核心(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);

//如下是輸出效果

這裡需要說下就是 createElement 方法, 當你想建立
複製代碼 代碼如下:
$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);

//如下是輸出效果


某一天


四、添加 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);

//如下是輸出效果

五、建立 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);

//如下是輸出效果







Label Value

最後我們來一個 完整複雜一點的例子:
複製代碼 代碼如下:
$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();

http://www.bkjia.com/PHPjc/824813.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/824813.htmlTechArticle在這一章節裡, 我們來瞭解下如何利用核心(core) PHP 產生 HTML 檔案 最近我在查詢 php.net 的時候,發現 DOMDocument 這個類非常的有意思, 可以...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.