国内流行的内容管理系统(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.

190 lines
5.6KB

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