helpers.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * This file is part of ibrand/EC-Open-Core.
  4. *
  5. * (c) 果酱社区 <https://guojiang.club>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. if (!function_exists('collect_to_array')) {
  11. /**
  12. * @param $collection
  13. *
  14. * @return array
  15. */
  16. function collect_to_array($collection)
  17. {
  18. $array = [];
  19. foreach ($collection as $item) {
  20. $array[] = $item;
  21. }
  22. return $array;
  23. }
  24. }
  25. if (!function_exists('export_csv')) {
  26. function export_csv($data, $head, $file_name = '')
  27. {
  28. set_time_limit(10000);
  29. ini_set('memory_limit', '300M');
  30. /*Storage::makeDirectory('public/exports');
  31. $path=storage_path('app/public/exports/') .$file_name.'.csv';*/
  32. $path = storage_path('exports') . '/' . $file_name . '.csv';
  33. $fp = fopen($path, 'w');
  34. foreach ($head as $i => $v) {
  35. $head[$i] = mb_convert_encoding($v, 'gbk', 'utf-8');
  36. }
  37. fputcsv($fp, $head);
  38. foreach ($data as $i => $v) {
  39. $row = [];
  40. foreach ($v as $key => $value) {
  41. $row[$key] = mb_convert_encoding($value, 'gbk', 'utf-8');
  42. }
  43. fputcsv($fp, $row);
  44. }
  45. fclose($fp);
  46. /*$result = '/storage/exports/' . $file_name . '.csv';*/
  47. return $path;
  48. }
  49. }
  50. if (!function_exists('isMobile')) {
  51. /**
  52. * isMobile函数:检测参数的值是否为正确的中国手机号码格式
  53. * 返回值:是正确的手机号码返回手机号码,不是返回false
  54. */
  55. function isMobile($Argv)
  56. {
  57. $RegExp = '/^(\+?0?86\-?)?((13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7})$/';
  58. return preg_match($RegExp, $Argv) ? $Argv : false;
  59. }
  60. }
  61. if (!function_exists('isMail')) {
  62. /**
  63. * isMail函数:检测是否为正确的邮件格式
  64. * 返回值:是正确的邮件格式返回邮件,不是返回false
  65. */
  66. function isMail($Argv)
  67. {
  68. $RegExp = '/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
  69. return preg_match($RegExp, $Argv) ? $Argv : false;
  70. }
  71. }