|  | 【代碼分享】我未完成的Node節點類 | 
                
          |   一派护法 十九级 | 
              【Node.php】<?php
 class Node implements ArrayAccess {
 protected $attributes = array();
 protected $children = array(); // Accessed by $this[number]
 protected $nodeName = 'div';
 
 protected static function attributeNameWarning() {
 triger_error('The name of the attribute cannot be an empty string.', E_USER_WARNING);
 }
 
 protected static function nodeNameWarning() {
 triger_error('The name of the node cannot be an empty string.', E_USER_WARNING);
 }
 /*static public function render($string) {
 }*/
 
 function __construct($nodeName = 'div') {
 $this->setNodeName($nodeName);
 }
 
 /* ------  Functions for ArrayAccess ------ */
 
 public function offsetExists($offset) {
 if (is_numeric($offset)) {
 return isset($this->children[$offset]);
 } else {
 return $this->hasAttribute($offset);
 }
 }
 
 public function offsetGet($offset) {
 if (is_numeric($offset)) {
 return $this->children[$offset];
 } else {
 return $this->getAttribute($offset);
 }
 }
 
 public function offsetSet($offset, $value) {
 if (is_numeric($offset)) {
 $this->children[$offset] = $value;
 } else {
 return $this->setAttribute($offset, $value);
 }
 }
 
 public function offsetUnset($offset) {
 if (is_numeric($offset)) {
 unset($this->children[$offset]);
 } else {
 $this->removeAttribute($offset);
 }
 }
 /* ------ END OF Functions for ArrayAccess ------ */
 
 public function getAttribute($name) {
 $name = trim($name);
 if (isset($this->attributes[$name])) {
 return $this->attributes[$name];
 } else {
 return '';
 }
 }
 
 public function getLength() {
 return count($this->children);
 }
 
 public function getNodeName() {
 return $this->nodeName;
 }
 
 public function hasAttribute($name) {
 if (empty($name)) {
 self::attributeNameWarning();
 return;
 }
 return isset($this->attributes[$name]);
 }
 
 public function removeAttribute($name) {
 if (empty($name)) {
 self::attributeNameWarning();
 return;
 }
 unset($this->attributes[$name]);
 }
 
 public function render($indentation = NULL, $noEndingSlash = false) {
 $str = $beginning = '';
 if (!is_null($indentation) && $indentation > 0) {
 $beginning = str_repeat('  ', $indentation);
 $str .= $beginning;
 }
 $str .= '<' . $this->nodeName;
 foreach ($this->attributes as $name => $value) {
 $str .= " $name";
 if (!is_null($value)) {
 $str .= "=\"$value\"";
 }
 }
 $len = $this->getLength();
 if ($len > 0) {
 $str .= '>';
 if ($len == 1 && is_string($this->children[0])) {
 $str .= $this->children[0];
 } else {
 $str .= "\n";
 foreach ($this->children as $child) {
 if ($child instanceof Node) {
 if (is_null($indentation)) {
 $newIndentation = null;
 } else {
 $newIndentation = $indentation + 1;
 }
 $child = $child->render($newIndentation, $noEndingSlash);
 } else {
 $child = "  {$beginning}{$child}\n";
 }
 $str .= $child;
 }
 $str .= $beginning;
 }
 $str .= '</' . $this->nodeName . '>';
 } else {
 if ($noEndingSlash) {
 $str .= '>';
 } else {
 $str .= ' />';
 }
 }
 $str .= "\n";
 return $str;
 }
 
 public function setAttribute($name, $value = NULL) {
 if (empty($name)) {
 self::attributeNameWarning();
 return;
 }
 $this->attributes[$name] = $value;
 }
 
 public function setNodeName($newNodeName) {
 if (empty($newNodeName)) {
 self::nodeNameWarning();
 return;
 }
 $this->nodeName = trim($newNodeName);
 }
 }
 
 | 
                
          |   一派护法 十九级 | 
              【HTMLNode.php】<?php
 class HTMLNode extends Node {
 public function getID() {
 return $this['id'];
 }
 
 public function removeID() {
 $this->removeAttribute('id');
 }
 
 public function render($indentation = 0, $noEndingSlash = true) {
 return parent::render($indentation, $noEndingSlash);
 }
 
 public function setID($newID) {
 if (is_null($newID)) {
 $this->removeID();
 } else {
 $this['id'] = $newID;
 }
 }
 }
 
 | 
                
          |   一派护法 十九级 | 
              【test.php】<?php
 require_once('classes/Node.php');
 require_once('classes/HTMLNode.php');
 ?>
 <!doctype html>
 <html>
 <head>
 <meta charset="utf-8">
 <title>Untitled Document</title>
 </head>
 
 <body>
 <?php
 $form = new HTMLNode('form');
 $form[0] = 'Here is some text.';
 $form->setID('Col');
 $form['action'] = 'a.php';
 $form[1] = new HTMLNode('h4');
 $form[1][0] = 'I\'m too sad!';
 $form[2] = new HTMLNode('p');
 $form[2][0] = 'Are you really ';
 $form[2][1] = new HTMLNode('span');
 $form[2][1][0] = 'sad';
 $form[2][1]['style'] = 'color:#FF0000;font-weight:bold';
 $form[2][2] = '?';
 echo $form->render();
 ?>
 </body>
 </html>
 
 | 
                
          |   一派护法 十九级 | 
              輸出:<!doctype html>
 <html>
 <head>
 <meta charset="utf-8">
 <title>Untitled Document</title>
 </head>
 
 <body>
 <form id="Col" action="a.php">
 Here is some text.
 <h4>I'm too sad!</h4>
 <p>
 Are you really
 <span style="color:#FF0000;font-weight:bold">sad</span>
 ?
 </p>
 </form>
 </body>
 </html>
 
 | 
                
          |   一派护法 十九级 | 
              【代碼解釋】$form = new HTMLNode('form'); // 創建一個form節點
 $form[0] = 'Here is some text.'; // form節點的的第0個子節點為文本節點,內容為Here is some text.
 $form->setID('Col'); // 設置form節點的id
 $form['action'] = 'a.php'; // 設置form節點的action屬性
 $form[1] = new HTMLNode('h4'); // form節點的的第1個子節點為一個h4節點
 $form[1][0] = 'I\'m too sad!'; // 設置這個h4節點的內容(也就是創建一個文本節點)
 $form[2] = new HTMLNode('p');
 $form[2][0] = 'Are you really ';
 $form[2][1] = new HTMLNode('span');
 $form[2][1][0] = 'sad';
 $form[2][1]['style'] = 'color:#FF0000;font-weight:bold';
 $form[2][2] = '?';
 echo $form->render(); // 輸出form節點的全部內容,并進行自動縮進
 
 | 
                
          |   一派护法 十九级 | 
              本代碼最重要的函數就是Node類的render方法。             | 
                
          |   一派护法 十九级 | 
              echo $form[2][1]->render();對這個span標籤單獨進行render:
 <span style="color:#FF0000;font-weight:bold">sad</span>
 
 
 | 
                
          |   一派护法 十九级 | 
              $span = $form[2][1]; $span[1] = new HTMLNode('a'); $a = $span[1]; $a[0] = '打開鏈接'; $a['target'] = '_blank'; $a['href'] = 'https://zh.arslanbar.net/post.php?t=23306 '; $a['style'] = 'color:#00FF00'; $a->setID('hahaha'); echo $span->render(); 上面的代碼把那個span標籤拿出來修改,加了一個a標籤,然後再單獨輸出這個span標籤。 輸出內容: <span style="color:#FF0000;font-weight:bold">   sad   <a target="_blank" href="https://zh.arslanbar.net/post.php?t=23306 " style="color:#00FF00" id="hahaha">打開鏈接</a> </span>             | 
                
          |   一派护法 十九级 | 
              回復:8樓 值得注意的是,由於$span是從$form中提取出來的,所以修改了$span也就修改了$form[2][1],因此執行echo $form->render();后輸出的內容已經發生變化: <form id="Col" action="a.php">   Here is some text.   <h4>I'm too sad!</h4>   <p>     Are you really      <span style="color:#FF0000;font-weight:bold">       sad       <a target="_blank" href="https://zh.arslanbar.net/post.php?t=23306 " style="color:#00FF00" id="hahaha">打開鏈接</a>     </span>     ?   </p> </form>             |