1. 1 <?php
  2. 2
  3. 3 /**
  4. 4 * 时间:2015-8-6
  5. 5 * 作者:River
  6. 6 * 超级有用、必须收藏的PHP代码样例
  7. 7 */
  8. 8 class Helper {
  9. 9
  10. 10 /**
  11. 11 * 友好显示var_dump
  12. 12 */
  13. 13 static public function dump($var, $echo = true, $label = null, $strict = true) {
  14. 14 $label = ( $label === null ) ? '' : rtrim($label) . ' ';
  15. 15 if (!$strict) {
  16. 16 if (ini_get('html_errors')) {
  17. 17 $output = print_r($var, true);
  18. 18 $output = "<pre>" . $label . htmlspecialchars($output, ENT_QUOTES) . "</pre>";
  19. 19 } else {
  20. 20 $output = $label . print_r($var, true);
  21. 21 }
  22. 22 } else {
  23. 23 ob_start();
  24. 24 var_dump($var);
  25. 25 $output = ob_get_clean();
  26. 26 if (!extension_loaded('xdebug')) {
  27. 27 $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
  28. 28 $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
  29. 29 }
  30. 30 }
  31. 31 if ($echo) {
  32. 32 echo $output;
  33. 33 return null;
  34. 34 } else
  35. 35 return $output;
  36. 36 }
  37. 37
  38. 38 /**
  39. 39 * 获取客户端IP地址
  40. 40 */
  41. 41 static public function getClientIP() {
  42. 42 static $ip = NULL;
  43. 43 if ($ip !== NULL)
  44. 44 return $ip;
  45. 45 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  46. 46 $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  47. 47 $pos = array_search('unknown', $arr);
  48. 48 if (false !== $pos)
  49. 49 unset($arr[$pos]);
  50. 50 $ip = trim($arr[0]);
  51. 51 } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  52. 52 $ip = $_SERVER['HTTP_CLIENT_IP'];
  53. 53 } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  54. 54 $ip = $_SERVER['REMOTE_ADDR'];
  55. 55 }
  56. 56 // IP地址合法验证
  57. 57 $ip = ( false !== ip2long($ip) ) ? $ip : '0.0.0.0';
  58. 58 return $ip;
  59. 59 }
  60. 60
  61. 61 /**
  62. 62 * 循环创建目录
  63. 63 */
  64. 64 static public function mkdir($dir, $mode = 0777) {
  65. 65 if (is_dir($dir) || @mkdir($dir, $mode))
  66. 66 return true;
  67. 67 if (!mk_dir(dirname($dir), $mode))
  68. 68 return false;
  69. 69 return @mkdir($dir, $mode);
  70. 70 }
  71. 71
  72. 72 /**
  73. 73 * 格式化单位
  74. 74 */
  75. 75 static public function byteFormat($size, $dec = 2) {
  76. 76 $a = array("B", "KB", "MB", "GB", "TB", "PB");
  77. 77 $pos = 0;
  78. 78 while ($size >= 1024) {
  79. 79 $size /= 1024;
  80. 80 $pos++;
  81. 81 }
  82. 82 return round($size, $dec) . " " . $a[$pos];
  83. 83 }
  84. 84
  85. 85 /**
  86. 86 * 下拉框,单选按钮 自动选择
  87. 87 *
  88. 88 * @param $string 输入字符
  89. 89 * @param $param 条件
  90. 90 * @param $type 类型
  91. 91 * selected checked
  92. 92 * @return string
  93. 93 */
  94. 94 static public function selected($string, $param = 1, $type = 'select') {
  95. 95
  96. 96 if (is_array($param)) {
  97. 97 $true = in_array($string, $param);
  98. 98 } elseif ($string == $param) {
  99. 99 $true = true;
  100. 100 }
  101. 101 if ($true)
  102. 102 $return = $type == 'select' ? 'selected="selected"' : 'checked="checked"';
  103. 103
  104. 104 echo $return;
  105. 105 }
  106. 106
  107. 107 /**
  108. 108 * 获得来源类型 post get
  109. 109 *
  110. 110 * @return unknown
  111. 111 */
  112. 112 static public function method() {
  113. 113 return strtoupper(isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET' );
  114. 114 }
  115. 115
  116. 116 /**
  117. 117 * 提示信息
  118. 118 */
  119. 119 static public function message($action = 'success', $content = '', $redirect = 'javascript:history.back(-1);', $timeout = 4) {
  120. 120
  121. 121 switch ($action) {
  122. 122 case 'success':
  123. 123 $titler = '操作完成';
  124. 124 $class = 'message_success';
  125. 125 $images = 'message_success.png';
  126. 126 break;
  127. 127 case 'error':
  128. 128 $titler = '操作未完成';
  129. 129 $class = 'message_error';
  130. 130 $images = 'message_error.png';
  131. 131 break;
  132. 132 case 'errorBack':
  133. 133 $titler = '操作未完成';
  134. 134 $class = 'message_error';
  135. 135 $images = 'message_error.png';
  136. 136 break;
  137. 137 case 'redirect':
  138. 138 header("Location:$redirect");
  139. 139 break;
  140. 140 case 'script':
  141. 141 if (empty($redirect)) {
  142. 142 exit('<script language="javascript">alert("' . $content . '");window.history.back(-1)</script>');
  143. 143 } else {
  144. 144 exit('<script language="javascript">alert("' . $content . '");window.location=" ' . $redirect . ' "</script>');
  145. 145 }
  146. 146 break;
  147. 147 }
  148. 148
  149. 149 // 信息头部
  150. 150 $header = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  151. 151 <html xmlns="http://www.w3.org/1999/xhtml">
  152. 152 <head>
  153. 153 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  154. 154 <title>操作提示</title>
  155. 155 <style type="text/css">
  156. 156 body{font:12px/1.7 "\5b8b\4f53",Tahoma;}
  157. 157 html,body,div,p,a,h3{margin:0;padding:0;}
  158. 158 .tips_wrap{ background:#F7FBFE;border:1px solid #DEEDF6;width:780px;padding:50px;margin:50px auto 0;}
  159. 159 .tips_inner{zoom:1;}
  160. 160 .tips_inner:after{visibility:hidden;display:block;font-size:0;content:" ";clear:both;height:0;}
  161. 161 .tips_inner .tips_img{width:80px;float:left;}
  162. 162 .tips_info{float:left;line-height:35px;width:650px}
  163. 163 .tips_info h3{font-weight:bold;color:#1A90C1;font-size:16px;}
  164. 164 .tips_info p{font-size:14px;color:#999;}
  165. 165 .tips_info p.message_error{font-weight:bold;color:#F00;font-size:16px; line-height:22px}
  166. 166 .tips_info p.message_success{font-weight:bold;color:#1a90c1;font-size:16px; line-height:22px}
  167. 167 .tips_info p.return{font-size:12px}
  168. 168 .tips_info .time{color:#f00; font-size:14px; font-weight:bold}
  169. 169 .tips_info p a{color:#1A90C1;text-decoration:none;}
  170. 170 </style>
  171. 171 </head>
  172. 172 <body>';
  173. 173 // 信息底部
  174. 174 $footer = '</body></html>';
  175. 175
  176. 176 $body = '<script type="text/javascript">
  177. 177 function delayURL(url) {
  178. 178 var delay = document.getElementById("time").innerHTML;
  179. 179 //alert(delay);
  180. 180 if(delay > 0){
  181. 181 delay--;
  182. 182 document.getElementById("time").innerHTML = delay;
  183. 183 } else {
  184. 184 window.location.href = url;
  185. 185 }
  186. 186 setTimeout("delayURL(\'" + url + "\')", 1000);
  187. 187 }
  188. 188 </script><div class="tips_wrap">
  189. 189 <div class="tips_inner">
  190. 190 <div class="tips_img">
  191. 191 <img src="' . Yii::app()->baseUrl . '/static/images/' . $images . '"/>
  192. 192 </div>
  193. 193 <div class="tips_info">
  194. 194 <p class="' . $class . '">' . $content . '</p>
  195. 195 <p class="return">系统自动跳转在 <span class="time" id="time">' . $timeout . ' </span> 秒后,如果不想等待,<a href="' . $redirect . '">点击这里跳转</a></p>
  196. 196 </div>
  197. 197 </div>
  198. 198 </div><script type="text/javascript">
  199. 199 delayURL("' . $redirect . '");
  200. 200 </script>';
  201. 201
  202. 202 exit($header . $body . $footer);
  203. 203 }
  204. 204
  205. 205 /**
  206. 206 * 查询字符生成
  207. 207 */
  208. 208 static public function buildCondition(array $getArray, array $keys = array()) {
  209. 209 if ($getArray) {
  210. 210 foreach ($getArray as $key => $value) {
  211. 211 if (in_array($key, $keys) && $value) {
  212. 212 $arr[$key] = CHtml::encode(strip_tags($value));
  213. 213 }
  214. 214 }
  215. 215 return $arr;
  216. 216 }
  217. 217 }
  218. 218
  219. 219 /**
  220. 220 * base64_encode
  221. 221 */
  222. 222 static function b64encode($string) {
  223. 223 $data = base64_encode($string);
  224. 224 $data = str_replace(array('+', '/', '='), array('-', '_', ''), $data);
  225. 225 return $data;
  226. 226 }
  227. 227
  228. 228 /**
  229. 229 * base64_decode
  230. 230 */
  231. 231 static function b64decode($string) {
  232. 232 $data = str_replace(array('-', '_'), array('+', '/'), $string);
  233. 233 $mod4 = strlen($data) % 4;
  234. 234 if ($mod4) {
  235. 235 $data .= substr('====', $mod4);
  236. 236 }
  237. 237 return base64_decode($data);
  238. 238 }
  239. 239
  240. 240 /**
  241. 241 * 验证邮箱
  242. 242 */
  243. 243 public static function email($str) {
  244. 244 if (empty($str))
  245. 245 return true;
  246. 246 $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
  247. 247 if (strpos($str, '@') !== false && strpos($str, '.') !== false) {
  248. 248 if (preg_match($chars, $str)) {
  249. 249 return true;
  250. 250 } else {
  251. 251 return false;
  252. 252 }
  253. 253 } else {
  254. 254 return false;
  255. 255 }
  256. 256 }
  257. 257
  258. 258 /**
  259. 259 * 验证手机号码
  260. 260 */
  261. 261 public static function mobile($str) {
  262. 262 if (empty($str)) {
  263. 263 return true;
  264. 264 }
  265. 265
  266. 266 return preg_match('#^13[\d]{9}$|14^[0-9]\d{8}|^15[0-9]\d{8}$|^18[0-9]\d{8}$#', $str);
  267. 267 }
  268. 268
  269. 269 /**
  270. 270 * 验证固定电话
  271. 271 */
  272. 272 public static function tel($str) {
  273. 273 if (empty($str)) {
  274. 274 return true;
  275. 275 }
  276. 276 return preg_match('/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/', trim($str));
  277. 277 }
  278. 278
  279. 279 /**
  280. 280 * 验证qq号码
  281. 281 */
  282. 282 public static function qq($str) {
  283. 283 if (empty($str)) {
  284. 284 return true;
  285. 285 }
  286. 286
  287. 287 return preg_match('/^[1-9]\d{4,12}$/', trim($str));
  288. 288 }
  289. 289
  290. 290 /**
  291. 291 * 验证邮政编码
  292. 292 */
  293. 293 public static function zipCode($str) {
  294. 294 if (empty($str)) {
  295. 295 return true;
  296. 296 }
  297. 297
  298. 298 return preg_match('/^[1-9]\d{5}$/', trim($str));
  299. 299 }
  300. 300
  301. 301 /**
  302. 302 * 验证ip
  303. 303 */
  304. 304 public static function ip($str) {
  305. 305 if (empty($str))
  306. 306 return true;
  307. 307
  308. 308 if (!preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $str)) {
  309. 309 return false;
  310. 310 }
  311. 311
  312. 312 $ip_array = explode('.', $str);
  313. 313
  314. 314 //真实的ip地址每个数字不能大于255(0-255)
  315. 315 return ( $ip_array[0] <= 255 && $ip_array[1] <= 255 && $ip_array[2] <= 255 && $ip_array[3] <= 255 ) ? true : false;
  316. 316 }
  317. 317
  318. 318 /**
  319. 319 * 验证身份证(中国)
  320. 320 */
  321. 321 public static function idCard($str) {
  322. 322 $str = trim($str);
  323. 323 if (empty($str))
  324. 324 return true;
  325. 325
  326. 326 if (preg_match("/^([0-9]{15}|[0-9]{17}[0-9a-z])$/i", $str))
  327. 327 return true;
  328. 328 else
  329. 329 return false;
  330. 330 }
  331. 331
  332. 332 /**
  333. 333 * 验证网址
  334. 334 */
  335. 335 public static function url($str) {
  336. 336 if (empty($str))
  337. 337 return true;
  338. 338
  339. 339 return preg_match('#(http|https|ftp|ftps)://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?#i', $str) ? true : false;
  340. 340 }
  341. 341
  342. 342 /**
  343. 343 * 根据ip获取地理位置
  344. 344 * @param $ip
  345. 345 * return :ip,beginip,endip,country,area
  346. 346 */
  347. 347 public static function getlocation($ip = '') {
  348. 348 $ip = new XIp();
  349. 349 $ipArr = $ip->getlocation($ip);
  350. 350 return $ipArr;
  351. 351 }
  352. 352
  353. 353 /**
  354. 354 * 中文转换为拼音
  355. 355 */
  356. 356 public static function pinyin($str) {
  357. 357 $ip = new XPinyin();
  358. 358 return $ip->output($str);
  359. 359 }
  360. 360
  361. 361 /**
  362. 362 * 拆分sql
  363. 363 *
  364. 364 * @param $sql
  365. 365 */
  366. 366 public static function splitsql($sql) {
  367. 367 $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=" . Yii::app()->db->charset, $sql);
  368. 368 $sql = str_replace("\r", "\n", $sql);
  369. 369 $ret = array();
  370. 370 $num = 0;
  371. 371 $queriesarray = explode(";\n", trim($sql));
  372. 372 unset($sql);
  373. 373 foreach ($queriesarray as $query) {
  374. 374 $ret[$num] = '';
  375. 375 $queries = explode("\n", trim($query));
  376. 376 $queries = array_filter($queries);
  377. 377 foreach ($queries as $query) {
  378. 378 $str1 = substr($query, 0, 1);
  379. 379 if ($str1 != '#' && $str1 != '-')
  380. 380 $ret[$num] .= $query;
  381. 381 }
  382. 382 $num++;
  383. 383 }
  384. 384 return ($ret);
  385. 385 }
  386. 386
  387. 387 /**
  388. 388 * 字符截取
  389. 389 *
  390. 390 * @param $string
  391. 391 * @param $length
  392. 392 * @param $dot
  393. 393 */
  394. 394 public static function cutstr($string, $length, $dot = '...', $charset = 'utf-8') {
  395. 395 if (strlen($string) <= $length)
  396. 396 return $string;
  397. 397
  398. 398 $pre = chr(1);
  399. 399 $end = chr(1);
  400. 400 $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array($pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end), $string);
  401. 401
  402. 402 $strcut = '';
  403. 403 if (strtolower($charset) == 'utf-8') {
  404. 404
  405. 405 $n = $tn = $noc = 0;
  406. 406 while ($n < strlen($string)) {
  407. 407
  408. 408 $t = ord($string[$n]);
  409. 409 if ($t == 9 || $t == 10 || ( 32 <= $t && $t <= 126 )) {
  410. 410 $tn = 1;
  411. 411 $n++;
  412. 412 $noc++;
  413. 413 } elseif (194 <= $t && $t <= 223) {
  414. 414 $tn = 2;
  415. 415 $n += 2;
  416. 416 $noc += 2;
  417. 417 } elseif (224 <= $t && $t <= 239) {
  418. 418 $tn = 3;
  419. 419 $n += 3;
  420. 420 $noc += 2;
  421. 421 } elseif (240 <= $t && $t <= 247) {
  422. 422 $tn = 4;
  423. 423 $n += 4;
  424. 424 $noc += 2;
  425. 425 } elseif (248 <= $t && $t <= 251) {
  426. 426 $tn = 5;
  427. 427 $n += 5;
  428. 428 $noc += 2;
  429. 429 } elseif ($t == 252 || $t == 253) {
  430. 430 $tn = 6;
  431. 431 $n += 6;
  432. 432 $noc += 2;
  433. 433 } else {
  434. 434 $n++;
  435. 435 }
  436. 436
  437. 437 if ($noc >= $length) {
  438. 438 break;
  439. 439 }
  440. 440 }
  441. 441 if ($noc > $length) {
  442. 442 $n -= $tn;
  443. 443 }
  444. 444
  445. 445 $strcut = substr($string, 0, $n);
  446. 446 } else {
  447. 447 for ($i = 0; $i < $length; $i++) {
  448. 448 $strcut .= ord($string[$i]) > 127 ? $string[$i] . $string[++$i] : $string[$i];
  449. 449 }
  450. 450 }
  451. 451
  452. 452 $strcut = str_replace(array($pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
  453. 453
  454. 454 $pos = strrpos($strcut, chr(1));
  455. 455 if ($pos !== false) {
  456. 456 $strcut = substr($strcut, 0, $pos);
  457. 457 }
  458. 458 return $strcut . $dot;
  459. 459 }
  460. 460
  461. 461 /**
  462. 462 * 描述格式化
  463. 463 * @param $subject
  464. 464 */
  465. 465 public static function clearCutstr($subject, $length = 0, $dot = '...', $charset = 'utf-8') {
  466. 466 if ($length) {
  467. 467 return XUtils::cutstr(strip_tags(str_replace(array("\r\n"), '', $subject)), $length, $dot, $charset);
  468. 468 } else {
  469. 469 return strip_tags(str_replace(array("\r\n"), '', $subject));
  470. 470 }
  471. 471 }
  472. 472
  473. 473 /**
  474. 474 * 检测是否为英文或英文数字的组合
  475. 475 *
  476. 476 * @return unknown
  477. 477 */
  478. 478 public static function isEnglist($param) {
  479. 479 if (!eregi("^[A-Z0-9]{1,26}$", $param)) {
  480. 480 return false;
  481. 481 } else {
  482. 482 return true;
  483. 483 }
  484. 484 }
  485. 485
  486. 486 /**
  487. 487 * 将自动判断网址是否加http://
  488. 488 *
  489. 489 * @param $http
  490. 490 * @return string
  491. 491 */
  492. 492 public static function convertHttp($url) {
  493. 493 if ($url == 'http://' || $url == '')
  494. 494 return '';
  495. 495
  496. 496 if (substr($url, 0, 7) != 'http://' && substr($url, 0, 8) != 'https://')
  497. 497 $str = 'http://' . $url;
  498. 498 else
  499. 499 $str = $url;
  500. 500 return $str;
  501. 501 }
  502. 502
  503. 503 /*
  504. 504 标题样式格式化
  505. 505 */
  506. 506
  507. 507 public static function titleStyle($style) {
  508. 508 $text = '';
  509. 509 if ($style['bold'] == 'Y') {
  510. 510 $text .='font-weight:bold;';
  511. 511 $serialize['bold'] = 'Y';
  512. 512 }
  513. 513
  514. 514 if ($style['underline'] == 'Y') {
  515. 515 $text .='text-decoration:underline;';
  516. 516 $serialize['underline'] = 'Y';
  517. 517 }
  518. 518
  519. 519 if (!empty($style['color'])) {
  520. 520 $text .='color:#' . $style['color'] . ';';
  521. 521 $serialize['color'] = $style['color'];
  522. 522 }
  523. 523
  524. 524 return array('text' => $text, 'serialize' => empty($serialize) ? '' : serialize($serialize));
  525. 525 }
  526. 526
  527. 527 // 自动转换字符集 支持数组转换
  528. 528 static public function autoCharset($string, $from = 'gbk', $to = 'utf-8') {
  529. 529 $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
  530. 530 $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
  531. 531 if (strtoupper($from) === strtoupper($to) || empty($string) || (is_scalar($string) && !is_string($string))) {
  532. 532 //如果编码相同或者非字符串标量则不转换
  533. 533 return $string;
  534. 534 }
  535. 535 if (is_string($string)) {
  536. 536 if (function_exists('mb_convert_encoding')) {
  537. 537 return mb_convert_encoding($string, $to, $from);
  538. 538 } elseif (function_exists('iconv')) {
  539. 539 return iconv($from, $to, $string);
  540. 540 } else {
  541. 541 return $string;
  542. 542 }
  543. 543 } elseif (is_array($string)) {
  544. 544 foreach ($string as $key => $val) {
  545. 545 $_key = self::autoCharset($key, $from, $to);
  546. 546 $string[$_key] = self::autoCharset($val, $from, $to);
  547. 547 if ($key != $_key)
  548. 548 unset($string[$key]);
  549. 549 }
  550. 550 return $string;
  551. 551 } else {
  552. 552 return $string;
  553. 553 }
  554. 554 }
  555. 555
  556. 556 /*
  557. 557 标题样式恢复
  558. 558 */
  559. 559
  560. 560 public static function titleStyleRestore($serialize, $scope = 'bold') {
  561. 561 $unserialize = unserialize($serialize);
  562. 562 if ($unserialize['bold'] == 'Y' && $scope == 'bold')
  563. 563 return 'Y';
  564. 564 if ($unserialize['underline'] == 'Y' && $scope == 'underline')
  565. 565 return 'Y';
  566. 566 if ($unserialize['color'] && $scope == 'color')
  567. 567 return $unserialize['color'];
  568. 568 }
  569. 569
  570. 570 /**
  571. 571 * 列出文件夹列表
  572. 572 *
  573. 573 * @param $dirname
  574. 574 * @return unknown
  575. 575 */
  576. 576 public static function getDir($dirname) {
  577. 577 $files = array();
  578. 578 if (is_dir($dirname)) {
  579. 579 $fileHander = opendir($dirname);
  580. 580 while (( $file = readdir($fileHander) ) !== false) {
  581. 581 $filepath = $dirname . '/' . $file;
  582. 582 if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0 || is_file($filepath)) {
  583. 583 continue;
  584. 584 }
  585. 585 $files[] = self::autoCharset($file, 'GBK', 'UTF8');
  586. 586 }
  587. 587 closedir($fileHander);
  588. 588 } else {
  589. 589 $files = false;
  590. 590 }
  591. 591 return $files;
  592. 592 }
  593. 593
  594. 594 /**
  595. 595 * 列出文件列表
  596. 596 *
  597. 597 * @param $dirname
  598. 598 * @return unknown
  599. 599 */
  600. 600 public static function getFile($dirname) {
  601. 601 $files = array();
  602. 602 if (is_dir($dirname)) {
  603. 603 $fileHander = opendir($dirname);
  604. 604 while (( $file = readdir($fileHander) ) !== false) {
  605. 605 $filepath = $dirname . '/' . $file;
  606. 606
  607. 607 if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0 || is_dir($filepath)) {
  608. 608 continue;
  609. 609 }
  610. 610 $files[] = self::autoCharset($file, 'GBK', 'UTF8');
  611. 611 ;
  612. 612 }
  613. 613 closedir($fileHander);
  614. 614 } else {
  615. 615 $files = false;
  616. 616 }
  617. 617 return $files;
  618. 618 }
  619. 619
  620. 620 /**
  621. 621 * [格式化图片列表数据]
  622. 622 *
  623. 623 * @return [type] [description]
  624. 624 */
  625. 625 public static function imageListSerialize($data) {
  626. 626
  627. 627 foreach ((array) $data['file'] as $key => $row) {
  628. 628 if ($row) {
  629. 629 $var[$key]['fileId'] = $data['fileId'][$key];
  630. 630 $var[$key]['file'] = $row;
  631. 631 }
  632. 632 }
  633. 633 return array('data' => $var, 'dataSerialize' => empty($var) ? '' : serialize($var));
  634. 634 }
  635. 635
  636. 636 /**
  637. 637 * 反引用一个引用字符串
  638. 638 * @param $string
  639. 639 * @return string
  640. 640 */
  641. 641 static function stripslashes($string) {
  642. 642 if (is_array($string)) {
  643. 643 foreach ($string as $key => $val) {
  644. 644 $string[$key] = self::stripslashes($val);
  645. 645 }
  646. 646 } else {
  647. 647 $string = stripslashes($string);
  648. 648 }
  649. 649 return $string;
  650. 650 }
  651. 651
  652. 652 /**
  653. 653 * 引用字符串
  654. 654 * @param $string
  655. 655 * @param $force
  656. 656 * @return string
  657. 657 */
  658. 658 static function addslashes($string, $force = 1) {
  659. 659 if (is_array($string)) {
  660. 660 foreach ($string as $key => $val) {
  661. 661 $string[$key] = self::addslashes($val, $force);
  662. 662 }
  663. 663 } else {
  664. 664 $string = addslashes($string);
  665. 665 }
  666. 666 return $string;
  667. 667 }
  668. 668
  669. 669 /**
  670. 670 * 格式化内容
  671. 671 */
  672. 672 static function formatHtml($content, $options = '') {
  673. 673 $purifier = new CHtmlPurifier();
  674. 674 if ($options != false)
  675. 675 $purifier->options = $options;
  676. 676 return $purifier->purify($content);
  677. 677 }
  678. 678
  679. 679 }
  680. 680
  681. 681 ?>

PHP代码样例的更多相关文章

  1. 33个超级有用必须要收藏的PHP代码样例

    作为一个正常的程序员,会好几种语言是十分正常的,相信大部分程序员也都会编写几句PHP程序,如果是WEB程序员,PHP一定是必备的,即使你没用开发过大型软件项目,也一定多少了解它的语法. 在PHP的流行 ...

  2. java servlet 代码样例 (demo)

    今天又搞了下jsp +servlet 的代码样例,感觉虽然搭了好多次,可是每次还是不记得那些参数,都要去网上搜索,索性自己把这次的简单demo给记录下来,供下次使用的时候直接复制吧. 这个web逻辑 ...

  3. zookeeper实战:SingleWorker代码样例

    我们需要一个“单点worker”系统,此系统来确保系统中定时任务在分布式环境中,任意时刻只有一个实例处于活跃:比如,生产环境中,有6台机器支撑一个应用,但是一个应用中有30个定时任务,这些任务有些必须 ...

  4. 30个php操作redis经常用法代码样例

    这篇文章主要介绍了30个php操作redis经常用法代码样例,本文事实上不止30个方法,能够操作string类型.list类型和set类型的数据,须要的朋友能够參考下 redis的操作非常多的,曾经看 ...

  5. JAVA各种OOM代码样例及解决方法

    周末了,觉得我还有很多作业没有写,针对目前大家对OOM的类型不太熟悉,那么我们来总结一下各种OOM出现的情况以及解决方法. 我们把各种OOM的情况列出来,然后逐一进行代码编写复现和提供解决方法. 1. ...

  6. 2020JAVA最新应对各种OOM代码样例及解决办法

    引言 作者:黄青石 链接:https://www.cnblogs.com/huangqingshi/p/13336648.html?utm_source=tuicool&utm_medium= ...

  7. Java操作HDFS代码样例

    代码在GitHub上. 包括如下几种样例代码: 新建文件夹 删除文件/文件夹 重命名文件/文件夹 查看指定路径下的所有文件 新建文件 读文件 写文件 下载文件至本地 上传本地文件 https://gi ...

  8. Python代码样例列表

    扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│       Python用户推荐系统曼哈顿算法实现.py│    ...

  9. 10个超级有用、必须收藏的PHP代码样例

    在PHP的流行普及中,网上总结出了很多实用的PHP代码片段,这些代码片段在当你遇到类似的问题时,粘贴过去就可以使用,非常的高效,非常的省时省力.将这些程序员前辈总结出的优秀代码放到自己的知识库中,是一 ...

随机推荐

  1. ceph 集群快速部署

    1.三台Centos7的主机 [root@ceph-1 ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)    2.主机 ...

  2. yii2.0 模态框简单使用

    1 <?php foreach($data as $model) :?> 2 3 <!-- 按钮触发模态框 --> 4 <button class="btn b ...

  3. 程序媛数据报告:近三年增长至70%,平均月薪1.54W,女性程序媛并不是特殊物种

  4. 下载配置VNC

    VNC通常使用连接图形化系统电脑可以安装了Gnome或者KDE yum autoremo ve tigervnc-server //移除 vncreboot //重启yum install tiger ...

  5. 第3.5节 丰富的Python字典操作

    一. 基本概念 Python提供一种通过名称来访问其各个值的数据结构,这种数据结构称为映射(mapping).字典(dict)是Python中唯一的内置映射类型,其中的值不按顺序排列,而是存储在键下, ...

  6. 第8.22节 Python案例详解:重写 “富比较”方法控制比较逻辑

    一. 案例说明 本节定义一个小汽车的类Car,类中包括车名carname.百公里油耗oilcostper100km.价格price三个属性.然后实现__lt__.__gt__.__le__.__ge_ ...

  7. 转:解析HTTP协议六种请求方法,get,head,put,delete,post有什么区别

    解析HTTP协议六种请求方法,get,head,put,delete,post有什么区别 标准Http协议支持六种请求方法,即: 1.GET 2.POST 3.PUT 4.Delete 5.HEAD ...

  8. 第10.2节 查看导入的Python模块

    在Python中,要查看导入模块,可以使用sys.modules来查看,不过sys包含了所有导入模块包括内建模块,如果需要过滤掉内建模块甚至扩展模块,则需要对sys.modules进行一下过滤. 一. ...

  9. Docker-使用数据卷在宿主机和容器间的数据共享

    场景一:现在用Docker创建了N个容器,但是这些容器之间需要数据共享,这个时候我们应该怎么办?[参考第四步] 场景二:docker创建了一个容器并进入容器,添加了一些定制功能,此时除了用docker ...

  10. pytorch实战(二)hw2——预测收入是否高于50000,分类问题

    代码和ppt: https://github.com/Iallen520/lhy_DL_Hw 遇到的一些细节问题: 1. X_train文件不带后缀名csv,所以不是规范的csv文件,不能直接用pd. ...