/public/admin-console/application/helpers/format.php

https://gitlab.com/vince.omega/mcb-nov-build · PHP · 150 lines · 115 code · 14 blank · 21 comment · 4 complexity · 62bf0f02b583e2a00f88353ac5420822 MD5 · raw file

  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Format helper class.
  4. *
  5. * $Id: format.php 4070 2009-03-11 20:37:38Z Geert $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class format_Core {
  13. /**
  14. * Formats a phone number according to the specified format.
  15. *
  16. * @param string phone number
  17. * @param string format string
  18. * @return string
  19. */
  20. function utf8_substr($str,$start)
  21. {
  22. preg_match_all("/./su", $str, $ar);
  23. if(func_num_args() >= 3) {
  24. $end = func_get_arg(2);
  25. return join("",array_slice($ar[0],$start,$end));
  26. } else {
  27. return join("",array_slice($ar[0],$start));
  28. }
  29. }
  30. public static function makeUrlFriendly($title)
  31. {
  32. //$output = utf8_substr($title, 0, 240);
  33. $output = strtolower($title);
  34. $output = preg_replace("/\s/" , "_", $output); //'/\s/e' Replace spaces with underscores
  35. $output = str_replace('_', '-', $output);
  36. $output = str_replace("&amp;", '', $output);
  37. //$output = str_replace("__", "_", $output);
  38. $output = str_replace("---", "-", $output);
  39. $output = str_replace("/", "", $output);
  40. $output = str_replace("\\", "", $output);
  41. $output = str_replace("'", "", $output);
  42. $output = str_replace(",", "", $output);
  43. $output = str_replace(";", "", $output);
  44. $output = str_replace(":", "", $output);
  45. $output = str_replace(".", "-", $output);
  46. $output = str_replace("?", "", $output);
  47. $output = str_replace("=", "-", $output);
  48. $output = str_replace("+", "", $output);
  49. $output = str_replace("$", "", $output);
  50. $output = str_replace("&", "", $output);
  51. $output = str_replace("!", "", $output);
  52. $output = str_replace(">>", "-", $output);
  53. $output = str_replace(">", "-", $output);
  54. $output = str_replace("<<", "-", $output);
  55. $output = str_replace("<", "-", $output);
  56. $output = str_replace("*", "", $output);
  57. $output = str_replace(")", "", $output);
  58. $output = str_replace("(", "", $output);
  59. $output = str_replace("[", "", $output);
  60. $output = str_replace("]", "", $output);
  61. $output = str_replace("^", "", $output);
  62. $output = str_replace("%", "", $output);
  63. $output = str_replace("#", "", $output);
  64. $output = str_replace("@", "", $output);
  65. $output = str_replace("`", "", $output);
  66. $output = str_replace("\"", "", $output);
  67. $output = str_replace("--", "-", $output);
  68. return $output;
  69. }
  70. public static function first_words($string, $num, $tail='&nbsp;'){
  71. $words = str_word_count($string, 2, "<>:/.");
  72. /*** get the first $num words ***/
  73. $firstwords = array_slice( $words, 0, $num);
  74. /** return words in a string **/
  75. return implode(' ', $firstwords).$tail;
  76. }
  77. public static function phone($number, $format = '3-3-4')
  78. {
  79. // Get rid of all non-digit characters in number string
  80. $number_clean = preg_replace('/\D+/', '', (string) $number);
  81. // Array of digits we need for a valid format
  82. $format_parts = preg_split('/[^1-9][^0-9]*/', $format, -1, PREG_SPLIT_NO_EMPTY);
  83. // Number must match digit count of a valid format
  84. if (strlen($number_clean) !== array_sum($format_parts))
  85. return $number;
  86. // Build regex
  87. $regex = '(\d{'.implode('})(\d{', $format_parts).'})';
  88. // Build replace string
  89. for ($i = 1, $c = count($format_parts); $i <= $c; $i++)
  90. {
  91. $format = preg_replace('/(?<!\$)[1-9][0-9]*/', '\$'.$i, $format, 1);
  92. }
  93. // Hocus pocus!
  94. return preg_replace('/^'.$regex.'$/', $format, $number_clean);
  95. }
  96. /**
  97. * Formats a URL to contain a protocol at the beginning.
  98. *
  99. * @param string possibly incomplete URL
  100. * @return string
  101. */
  102. public static function url($str = '')
  103. {
  104. // Clear protocol-only strings like "http://"
  105. if ($str === '' OR substr($str, -3) === '://')
  106. return '';
  107. // If no protocol given, prepend "http://" by default
  108. if (strpos($str, '://') === FALSE)
  109. return 'http://'.$str;
  110. // Return the original URL
  111. return $str;
  112. }
  113. public static function dollar($amount)
  114. {
  115. return '$' . number_format($amount, 2);
  116. }
  117. public static function format_date($date)
  118. {
  119. if (!is_numeric($date))
  120. $date = strtotime($date);
  121. return date("m/d/Y", $date);
  122. }
  123. public static function dollar_amount($dollar){
  124. return '$'. number_format($dollar, 2);
  125. }
  126. } // End format