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

246 lines
6.5KB

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