Home  >  Article  >  Backend Development  >  Two functions of PHP: generating serial numbers and string replacement

Two functions of PHP: generating serial numbers and string replacement

WBOY
WBOYOriginal
2016-07-25 09:05:551345browse
  1. /**

  2. * Serial number generator
  3. func: snMaker
  4. param: $pre = ''
  5. */
  6. function snMaker($pre = '') {
  7. $date = date('Ymd');
  8. $rand = rand(1000000,9999999);
  9. $time = mb_substr(time(), 5, 5, 'utf-8');
  10. $serialNumber = $pre.$date.$time.$rand;
  11. // echo strlen($serialNumber).'
    ';
  12. return $serialNumber;
  13. }
  14. echo snMaker();

  15. /**

  16. * Replace part of a string with a specific character
  17. * @param str or int $str The string to be processed
  18. * @param str or int $to What string will be replaced?
  19. * @param int $start Keep the first few characters
  20. * @param int $end Keep the last few characters
  21. * url: http://bbs.it-home.org
  22. * date: 2013/2/17
  23. */
  24. function hideString($str = 'hello', $to = '*', $start = 1, $end = 0) {
  25. $lenth = strlen($str) - $start - $end;
  26. $lenth = ($lenth < 0) ? 0 : $lenth;
  27. $to = str_repeat($to, $lenth);
  28. $str = substr_replace($str, $to, $start, $lenth);
  29. return $str;
  30. }
  31. echo hideString();
  32. ?>

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn