src/Helpers/Encoder.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Helpers;
  3. class Encoder {
  4. public static function formatUrl($str,$delimiter = '-') {
  5. if (!empty($replace)) {
  6. $str = str_replace((array) $replace, ' ', $str);
  7. }
  8. $str = htmlentities($str, ENT_NOQUOTES, 'UTF-8');
  9. $str = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str);
  10. $str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); // pour les ligatures e.g. '&oelig;'
  11. $str = preg_replace('#&[^;]+;#', '', $str); // supprime les autres caractères
  12. $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
  13. $clean = strtolower(trim($clean, '-'));
  14. $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
  15. return $clean;
  16. }
  17. public static function toUppercase($str,$delimiter = '-') {
  18. if (!empty($replace)) {
  19. $str = str_replace((array) $replace, ' ', $str);
  20. }
  21. $str = htmlentities($str, ENT_NOQUOTES, 'UTF-8');
  22. $str = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str);
  23. $str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); // pour les ligatures e.g. '&oelig;'
  24. $str = preg_replace('#&[^;]+;#', '', $str); // supprime les autres caractères
  25. $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
  26. return strtoupper($clean);
  27. }
  28. public static function fputcsvEol($handle, $array, $delimiter = ';', $enclosure = '"') {
  29. return fputcsv($handle, $array, $delimiter, $enclosure);
  30. if($return !== FALSE && "\n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
  31. fwrite($handle, $eol);
  32. }
  33. return $return;
  34. return fputs($handle, implode($delimiter, array_map(function($n) {return static::encodeCsv($n);}, $array))."\r\n");
  35. }
  36. public static function encodeCsv($value) {
  37. ///remove any ESCAPED double quotes within string.
  38. $value = str_replace('\\"','"',$value);
  39. //then force escape these same double quotes And Any UNESCAPED Ones.
  40. $value = str_replace('"','\"',$value);
  41. //force wrap value in quotes and return
  42. return '"'.$value.'"';
  43. }
  44. public static function getExtension($fname) {
  45. $tmp = explode('.',$fname);
  46. return $tmp[count($tmp)-1];
  47. }
  48. public static function getRandomStr($n) {
  49. $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  50. $randomStr = '';
  51. for ($i = 0; $i < $n; $i++) {
  52. $index = rand(0, strlen($str) - 1);
  53. $randomStr .= $str[$index];
  54. }
  55. return $randomStr;
  56. }
  57. public static function getRandomDigit($n) {
  58. $str = '0123456789';
  59. $randomStr = '';
  60. for ($i = 0; $i < $n; $i++) {
  61. $index = rand(0, strlen($str) - 1);
  62. $randomStr .= $str[$index];
  63. }
  64. return $randomStr;
  65. }
  66. public static function stripTags($str,$tags=["a"]) {
  67. $tags_to_strip = Array("p","font");
  68. foreach ($tags as $tag)
  69. {
  70. $str = preg_replace("/<\\/?" . $tag . "(.|\\s)*?>/",'',$str);
  71. }
  72. return $str;
  73. }
  74. public static function priceFormat($price) {
  75. return number_format($price, 2, ".", "");
  76. }
  77. public static function roundPrice($price) {
  78. return round($price, 2);
  79. }
  80. }
  81. //