国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
6.0KB

  1. <?php
  2. /**
  3. * 属性的数据描述
  4. *
  5. * @version $Id: dedeatt.class.php 1 13:50 2010年7月6日Z tianya $
  6. * @package DedeBIZ.Libraries
  7. * @copyright Copyright (c) 2020, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. // ------------------------------------------------------------------------
  12. /**
  13. * 属性的数据描述
  14. * function c____DedeAtt();
  15. *
  16. * @package DedeAtt
  17. * @subpackage DedeBIZ.Libraries
  18. * @link https://www.dedebiz.com
  19. */
  20. class DedeAtt
  21. {
  22. var $Count = -1;
  23. var $Items = array(); //属性元素的集合
  24. /**
  25. * //获得某个属性
  26. *
  27. * @access public
  28. * @param string $str 名称
  29. * @return string
  30. */
  31. function GetAtt($str)
  32. {
  33. if ($str == "") {
  34. return "";
  35. }
  36. if (isset($this->Items[$str])) {
  37. return $this->Items[$str];
  38. } else {
  39. return "";
  40. }
  41. }
  42. //同上
  43. function GetAttribute($str)
  44. {
  45. return $this->GetAtt($str);
  46. }
  47. /**
  48. * 判断属性是否存在
  49. *
  50. * @access public
  51. * @param string $str 属性名称
  52. * @return string
  53. */
  54. function IsAttribute($str)
  55. {
  56. return isset($this->Items[$str]) ? TRUE : FALSE;
  57. }
  58. /**
  59. * 获得标记名称
  60. *
  61. * @access public
  62. * @return string
  63. */
  64. function GetTagName()
  65. {
  66. return $this->GetAtt("tagname");
  67. }
  68. /**
  69. * 获得属性个数
  70. *
  71. * @access public
  72. * @return int
  73. */
  74. function GetCount()
  75. {
  76. return $this->Count + 1;
  77. }
  78. } //End DedeAtt
  79. /**
  80. * 属性解析器
  81. * function c____DedeAttParse();
  82. *
  83. * @package DedeAtt
  84. * @subpackage DedeBIZ.Libraries
  85. * @link https://www.dedebiz.com
  86. */
  87. class DedeAttParse
  88. {
  89. var $SourceString = "";
  90. var $SourceMaxSize = 1024;
  91. var $CAtt; //属性的数据描述类
  92. var $CharToLow = TRUE;
  93. /**
  94. * 设置属性解析器源字符串
  95. *
  96. * @access public
  97. * @param string $str 需要解析的字符串
  98. * @return string
  99. */
  100. function SetSource($str = "")
  101. {
  102. $this->CAtt = new DedeAtt();
  103. $strLen = 0;
  104. $this->SourceString = trim(preg_replace("/[ \t\r\n]{1,}/", " ", $str));
  105. $strLen = strlen($this->SourceString);
  106. if ($strLen > 0 && $strLen <= $this->SourceMaxSize) {
  107. $this->ParseAtt();
  108. }
  109. }
  110. /**
  111. * 解析属性(私有成员,仅给SetSource调用)
  112. *
  113. * @access private
  114. * @return void
  115. */
  116. function ParseAtt()
  117. {
  118. $d = "";
  119. $tmpatt = "";
  120. $tmpvalue = "";
  121. $startdd = -1;
  122. $ddtag = "";
  123. $notAttribute = TRUE;
  124. $strLen = strlen($this->SourceString);
  125. // 这里是获得Tag的名称,可视情况是否需要
  126. // 如果不在这个里解析,则在解析整个Tag时解析
  127. // 属性中不应该存在tagname这个名称
  128. for ($i = 0; $i < $strLen; $i++) {
  129. $d = substr($this->SourceString, $i, 1);
  130. if ($d == ' ') {
  131. $this->CAtt->Count++;
  132. if ($this->CharToLow) {
  133. $this->CAtt->Items["tagname"] = strtolower(trim($tmpvalue));
  134. } else {
  135. $this->CAtt->Items["tagname"] = trim($tmpvalue);
  136. }
  137. $tmpvalue = "";
  138. $notAttribute = FALSE;
  139. break;
  140. } else {
  141. $tmpvalue .= $d;
  142. }
  143. }
  144. //不存在属性列表的情况
  145. if ($notAttribute) {
  146. $this->CAtt->Count++;
  147. $this->CAtt->Items["tagname"] = ($this->CharToLow ? strtolower(trim($tmpvalue)) : trim($tmpvalue));
  148. }
  149. //如果字符串含有属性值,遍历源字符串,并获得各属性
  150. if (!$notAttribute) {
  151. for ($i; $i < $strLen; $i++) {
  152. $d = substr($this->SourceString, $i, 1);
  153. if ($startdd == -1) {
  154. if ($d != "=") {
  155. $tmpatt .= $d;
  156. } else {
  157. if ($this->CharToLow) {
  158. $tmpatt = strtolower(trim($tmpatt));
  159. } else {
  160. $tmpatt = trim($tmpatt);
  161. }
  162. $startdd = 0;
  163. }
  164. } else if ($startdd == 0) {
  165. switch ($d) {
  166. case ' ':
  167. // continue;
  168. break;
  169. case '\'':
  170. $ddtag = '\'';
  171. $startdd = 1;
  172. break;
  173. case '"':
  174. $ddtag = '"';
  175. $startdd = 1;
  176. break;
  177. default:
  178. $tmpvalue .= $d;
  179. $ddtag = ' ';
  180. $startdd = 1;
  181. break;
  182. }
  183. } else if ($startdd == 1) {
  184. if ($d == $ddtag) {
  185. $this->CAtt->Count++;
  186. $this->CAtt->Items[$tmpatt] = trim($tmpvalue); //strtolower(trim($tmpvalue));
  187. $tmpatt = "";
  188. $tmpvalue = "";
  189. $startdd = -1;
  190. } else {
  191. $tmpvalue .= $d;
  192. }
  193. }
  194. }
  195. if ($tmpatt != "") {
  196. $this->CAtt->Count++;
  197. $this->CAtt->Items[$tmpatt] = trim($tmpvalue); //strtolower(trim($tmpvalue));
  198. } //完成属性解析
  199. } //for
  200. } //has Attribute
  201. }//End DedeAttParse