国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

336 wiersze
10KB

  1. <?php if(!defined('DEDEINC')) exit('dedecms');
  2. /**
  3. * 字符串小助手
  4. *
  5. * @version $Id: string.helper.php 5 14:24 2010年7月5日Z tianya $
  6. * @package DedeCMS.Helpers
  7. * @copyright Copyright (c) 2020, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. //拼音的缓冲数组
  12. $pinyins = Array();
  13. /**
  14. * 中文截取2,单字节截取模式
  15. * 如果是request的内容,必须使用这个函数
  16. *
  17. * @access public
  18. * @param string $str 需要截取的字符串
  19. * @param int $slen 截取的长度
  20. * @param int $startdd 开始标记处
  21. * @return string
  22. */
  23. if ( ! function_exists('cn_substrR'))
  24. {
  25. function cn_substrR($str, $slen, $startdd=0)
  26. {
  27. $str = cn_substr(stripslashes($str), $slen, $startdd);
  28. return addslashes($str);
  29. }
  30. }
  31. /**
  32. * 中文截取2,单字节截取模式
  33. *
  34. * @access public
  35. * @param string $str 需要截取的字符串
  36. * @param int $slen 截取的长度
  37. * @param int $startdd 开始标记处
  38. * @return string
  39. */
  40. if ( ! function_exists('cn_substr'))
  41. {
  42. function cn_substr($str, $slen, $startdd=0)
  43. {
  44. global $cfg_soft_lang;
  45. if($cfg_soft_lang=='utf-8')
  46. {
  47. return cn_substr_utf8($str, $slen, $startdd);
  48. }
  49. $restr = '';
  50. $c = '';
  51. $str_len = strlen($str);
  52. if($str_len < $startdd+1)
  53. {
  54. return '';
  55. }
  56. if($str_len < $startdd + $slen || $slen==0)
  57. {
  58. $slen = $str_len - $startdd;
  59. }
  60. $enddd = $startdd + $slen - 1;
  61. for($i=0;$i<$str_len;$i++)
  62. {
  63. if($startdd==0)
  64. {
  65. $restr .= $c;
  66. }
  67. else if($i > $startdd)
  68. {
  69. $restr .= $c;
  70. }
  71. if(ord($str[$i])>0x80)
  72. {
  73. if($str_len>$i+1)
  74. {
  75. $c = $str[$i].$str[$i+1];
  76. }
  77. $i++;
  78. }
  79. else
  80. {
  81. $c = $str[$i];
  82. }
  83. if($i >= $enddd)
  84. {
  85. if(strlen($restr)+strlen($c)>$slen)
  86. {
  87. break;
  88. }
  89. else
  90. {
  91. $restr .= $c;
  92. break;
  93. }
  94. }
  95. }
  96. return $restr;
  97. }
  98. }
  99. /**
  100. * utf-8中文截取,单字节截取模式
  101. *
  102. * @access public
  103. * @param string $str 需要截取的字符串
  104. * @param int $slen 截取的长度
  105. * @param int $startdd 开始标记处
  106. * @return string
  107. */
  108. if ( ! function_exists('cn_substr_utf8'))
  109. {
  110. function cn_substr_utf8($str, $length, $start=0)
  111. {
  112. if(strlen($str) < $start+1)
  113. {
  114. return '';
  115. }
  116. preg_match_all("/./su", $str, $ar);
  117. $str = '';
  118. $tstr = '';
  119. //为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取
  120. for($i=0; isset($ar[0][$i]); $i++)
  121. {
  122. if(strlen($tstr) < $start)
  123. {
  124. $tstr .= $ar[0][$i];
  125. }
  126. else
  127. {
  128. if(strlen($str) < $length + strlen($ar[0][$i]) )
  129. {
  130. $str .= $ar[0][$i];
  131. }
  132. else
  133. {
  134. break;
  135. }
  136. }
  137. }
  138. return $str;
  139. }
  140. }
  141. /**
  142. * HTML转换为文本
  143. *
  144. * @param string $str 需要转换的字符串
  145. * @param string $r 如果$r=0直接返回内容,否则需要使用反斜线引用字符串
  146. * @return string
  147. */
  148. if ( ! function_exists('Html2Text'))
  149. {
  150. function Html2Text($str,$r=0)
  151. {
  152. if(!function_exists('SpHtml2Text'))
  153. {
  154. require_once(DEDEINC."/inc/inc_fun_funString.php");
  155. }
  156. if($r==0)
  157. {
  158. return SpHtml2Text($str);
  159. }
  160. else
  161. {
  162. $str = SpHtml2Text(stripslashes($str));
  163. return addslashes($str);
  164. }
  165. }
  166. }
  167. /**
  168. * 文本转HTML
  169. *
  170. * @param string $txt 需要转换的文本内容
  171. * @return string
  172. */
  173. if ( ! function_exists('Text2Html'))
  174. {
  175. function Text2Html($txt)
  176. {
  177. $txt = str_replace(" ", " ", $txt);
  178. $txt = str_replace("<", "&lt;", $txt);
  179. $txt = str_replace(">", "&gt;", $txt);
  180. $txt = preg_replace("/[\r\n]{1,}/isU", "<br/>\r\n", $txt);
  181. return $txt;
  182. }
  183. }
  184. /**
  185. * 获取半角字符
  186. *
  187. * @param string $fnum 数字字符串
  188. * @return string
  189. */
  190. if ( ! function_exists('GetAlabNum'))
  191. {
  192. function GetAlabNum($fnum)
  193. {
  194. $nums = array("0","1","2","3","4","5","6","7","8","9");
  195. //$fnums = "0123456789";
  196. $fnums = array("0","1","2","3","4","5","6","7","8","9");
  197. $fnum = str_replace($nums, $fnums, $fnum);
  198. $fnum = preg_replace("/[^0-9\.-]/", '', $fnum);
  199. if($fnum=='')
  200. {
  201. $fnum=0;
  202. }
  203. return $fnum;
  204. }
  205. }
  206. /**
  207. * 获取拼音以gbk编码为准
  208. *
  209. * @access public
  210. * @param string $str 字符串信息
  211. * @param int $ishead 是否取头字母
  212. * @param int $isclose 是否关闭字符串资源
  213. * @return string
  214. */
  215. if ( ! function_exists('GetPinyin'))
  216. {
  217. function GetPinyin($str, $ishead=0, $isclose=1)
  218. {
  219. global $cfg_soft_lang;
  220. if(!function_exists('SpGetPinyin'))
  221. {
  222. //全局函数仅是inc_fun_funAdmin.php文件中函数的一个映射
  223. require_once(DEDEINC."/inc/inc_fun_funAdmin.php");
  224. }
  225. if($cfg_soft_lang=='utf-8')
  226. {
  227. return SpGetPinyin(utf82gb($str), $ishead, $isclose);
  228. }
  229. else
  230. {
  231. return SpGetPinyin($str, $ishead, $isclose);
  232. }
  233. }
  234. }
  235. /**
  236. * 将实体html代码转换成标准html代码(兼容php4)
  237. *
  238. * @access public
  239. * @param string $str 字符串信息
  240. * @param long $options 替换的字符集
  241. * @return string
  242. */
  243. if ( ! function_exists('htmlspecialchars_decode'))
  244. {
  245. function htmlspecialchars_decode($str, $options=ENT_COMPAT) {
  246. $trans = get_html_translation_table(HTML_SPECIALCHARS, $options);
  247. $decode = ARRAY();
  248. foreach ($trans AS $char=>$entity) {
  249. $decode[$entity] = $char;
  250. }
  251. $str = strtr($str, $decode);
  252. return $str;
  253. }
  254. }
  255. if ( ! function_exists('ubb'))
  256. {
  257. function ubb($Text) {
  258. $Text=trim($Text);
  259. //$Text=htmlspecialchars($Text);
  260. //$Text=ereg_replace("\n","<br>",$Text);
  261. $Text=preg_replace("/\\t/is"," ",$Text);
  262. $Text=preg_replace("/\[hr\]/is","<hr>",$Text);
  263. $Text=preg_replace("/\[separator\]/is","<br/>",$Text);
  264. $Text=preg_replace("/\[h1\](.+?)\[\/h1\]/is","<h1>\\1</h1>",$Text);
  265. $Text=preg_replace("/\[h2\](.+?)\[\/h2\]/is","<h2>\\1</h2>",$Text);
  266. $Text=preg_replace("/\[h3\](.+?)\[\/h3\]/is","<h3>\\1</h3>",$Text);
  267. $Text=preg_replace("/\[h4\](.+?)\[\/h4\]/is","<h4>\\1</h4>",$Text);
  268. $Text=preg_replace("/\[h5\](.+?)\[\/h5\]/is","<h5>\\1</h5>",$Text);
  269. $Text=preg_replace("/\[h6\](.+?)\[\/h6\]/is","<h6>\\1</h6>",$Text);
  270. $Text=preg_replace("/\[center\](.+?)\[\/center\]/is","<center>\\1</center>",$Text);
  271. //$Text=preg_replace("/\[url=([^\[]*)\](.+?)\[\/url\]/is","<a href=\\1 target='_blank'>\\2</a>",$Text);
  272. $Text=preg_replace("/\[url\](.+?)\[\/url\]/is","<a href=\"\\1\" target='_blank'>\\1</a>",$Text);
  273. $Text=preg_replace("/\[url=(http:\/\/.+?)\](.+?)\[\/url\]/is","<a href='\\1' target='_blank'>\\2</a>",$Text);
  274. $Text=preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is","<a href=\\1>\\2</a>",$Text);
  275. $Text=preg_replace("/\[img\](.+?)\[\/img\]/is","<img src=\\1>",$Text);
  276. $Text=preg_replace("/\[img\s(.+?)\](.+?)\[\/img\]/is","<img \\1 src=\\2>",$Text);
  277. $Text=preg_replace("/\[color=(.+?)\](.+?)\[\/color\]/is","<font color=\\1>\\2</font>",$Text);
  278. $Text=preg_replace("/\[style=(.+?)\](.+?)\[\/style\]/is","<div class='\\1'>\\2</div>",$Text);
  279. $Text=preg_replace("/\[size=(.+?)\](.+?)\[\/size\]/is","<font size=\\1>\\2</font>",$Text);
  280. $Text=preg_replace("/\[sup\](.+?)\[\/sup\]/is","<sup>\\1</sup>",$Text);
  281. $Text=preg_replace("/\[sub\](.+?)\[\/sub\]/is","<sub>\\1</sub>",$Text);
  282. $Text=preg_replace("/\[pre\](.+?)\[\/pre\]/is","<pre>\\1</pre>",$Text);
  283. if (version_compare(PHP_VERSION, '5.5.0', '>='))
  284. {
  285. $Text=preg_replace_callback("/\[colorTxt\](.+?)\[\/colorTxt\]/is","color_txt",$Text);
  286. } else {
  287. $Text=preg_replace("/\[colorTxt\](.+?)\[\/colorTxt\]/eis","color_txt('\\1')",$Text);
  288. }
  289. $Text=preg_replace("/\[email\](.+?)\[\/email\]/is","<a href='mailto:\\1'>\\1</a>",$Text);
  290. $Text=preg_replace("/\[i\](.+?)\[\/i\]/is","<i>\\1</i>",$Text);
  291. $Text=preg_replace("/\[u\](.+?)\[\/u\]/is","<u>\\1</u>",$Text);
  292. $Text=preg_replace("/\[b\](.+?)\[\/b\]/is","<b>\\1</b>",$Text);
  293. $Text=preg_replace("/\[quote\](.+?)\[\/quote\]/is","<blockquote>引用:<div style='border:1px solid silver;background:#EFFFDF;color:#393939;padding:5px' >\\1</div></blockquote>", $Text);
  294. $Text=preg_replace("/\[sig\](.+?)\[\/sig\]/is","<div style='text-align: left; color: darkgreen; margin-left: 5%'><br><br>--------------------------<br>\\1<br>--------------------------</div>", $Text);
  295. return $Text;
  296. }
  297. }
  298. if ( !function_exists('color_txt') )
  299. {
  300. function color_txt($str){
  301. if ( is_array($str) )
  302. {
  303. $str = $str[1];
  304. }
  305. $len = mb_strlen($str);
  306. $colorTxt = '';
  307. for($i=0; $i<$len; $i++) {
  308. $colorTxt .= '<span style="color:'.rand_color().'">'.mb_substr($str,$i,1,'utf-8').'</span>';
  309. }
  310. return $colorTxt;
  311. }
  312. }
  313. if ( !function_exists('rand_color') )
  314. {
  315. function rand_color(){
  316. return '#'.sprintf("%02X",mt_rand(0,255)).sprintf("%02X",mt_rand(0,255)).sprintf("%02X",mt_rand(0,255));
  317. }
  318. }