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

201 lines
5.9KB

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