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

296 lines
10KB

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