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

297 lines
10KB

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