You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
7.2KB

  1. <?php
  2. namespace Lc\SovBundle\Component;
  3. use Cocur\Slugify\Slugify;
  4. class StringComponent
  5. {
  6. public function limitText($text, $limit)
  7. {
  8. $text = strip_tags($text);
  9. if (str_word_count($text, 0) > $limit) {
  10. $words = str_word_count($text, 2);
  11. $pos = array_keys($words);
  12. $text = substr($text, 0, $pos[$limit]) . '...';
  13. }
  14. return $text;
  15. }
  16. public function limitTextByLength($text, $length, $append = '...')
  17. {
  18. if (strlen($text) > $length) {
  19. $text = substr($text, 0, $length) . $append;
  20. }
  21. return $text;
  22. }
  23. function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true)
  24. {
  25. if ($considerHtml) {
  26. // if the plain text is shorter than the maximum length, return the whole text
  27. if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
  28. return $text;
  29. }
  30. // splits all html-tags to scanable lines
  31. preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
  32. $total_length = strlen($ending);
  33. $open_tags = array();
  34. $truncate = '';
  35. foreach ($lines as $line_matchings) {
  36. // if there is any html-tag in this line, handle it and add it (uncounted) to the output
  37. if (!empty($line_matchings[1])) {
  38. // if it's an "empty element" with or without xhtml-conform closing slash
  39. if (preg_match(
  40. '/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is',
  41. $line_matchings[1]
  42. )) {
  43. // do nothing
  44. // if tag is a closing tag
  45. } else {
  46. if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
  47. // delete tag from $open_tags list
  48. $pos = array_search($tag_matchings[1], $open_tags);
  49. if ($pos !== false) {
  50. unset($open_tags[$pos]);
  51. }
  52. // if tag is an opening tag
  53. } else {
  54. if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
  55. // add tag to the beginning of $open_tags list
  56. array_unshift($open_tags, strtolower($tag_matchings[1]));
  57. }
  58. }
  59. }
  60. // add html-tag to $truncate'd text
  61. $truncate .= $line_matchings[1];
  62. }
  63. // calculate the length of the plain text part of the line; handle entities as one character
  64. $content_length = strlen(
  65. preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])
  66. );
  67. if ($total_length + $content_length > $length) {
  68. // the number of characters which are left
  69. $left = $length - $total_length;
  70. $entities_length = 0;
  71. // search for html entities
  72. if (preg_match_all(
  73. '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i',
  74. $line_matchings[2],
  75. $entities,
  76. PREG_OFFSET_CAPTURE
  77. )) {
  78. // calculate the real length of all entities in the legal range
  79. foreach ($entities[0] as $entity) {
  80. if ($entity[1] + 1 - $entities_length <= $left) {
  81. $left--;
  82. $entities_length += strlen($entity[0]);
  83. } else {
  84. // no more characters left
  85. break;
  86. }
  87. }
  88. }
  89. $truncate .= substr($line_matchings[2], 0, $left + $entities_length);
  90. // maximum lenght is reached, so get off the loop
  91. break;
  92. } else {
  93. $truncate .= $line_matchings[2];
  94. $total_length += $content_length;
  95. }
  96. // if the maximum length is reached, get off the loop
  97. if ($total_length >= $length) {
  98. break;
  99. }
  100. }
  101. } else {
  102. if (strlen($text) <= $length) {
  103. return $text;
  104. } else {
  105. $truncate = substr($text, 0, $length - strlen($ending));
  106. }
  107. }
  108. // if the words shouldn't be cut in the middle...
  109. if (!$exact) {
  110. // ...search the last occurance of a space...
  111. $spacepos = strrpos($truncate, ' ');
  112. if (isset($spacepos)) {
  113. // ...and cut the text in this position
  114. $truncate = substr($truncate, 0, $spacepos);
  115. }
  116. }
  117. // add the defined ending to the text
  118. $truncate .= $ending;
  119. if ($considerHtml) {
  120. // close all unclosed html-tags
  121. foreach ($open_tags as $tag) {
  122. $truncate .= '</' . $tag . '>';
  123. }
  124. }
  125. return $truncate;
  126. }
  127. function stripAccents($stripAccents)
  128. {
  129. return strtr(
  130. $stripAccents,
  131. 'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ',
  132. 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY'
  133. );
  134. }
  135. function cleanStringToCompare($string)
  136. {
  137. return $this->stripAccents(trim(strtolower($string)));
  138. }
  139. public function slugify($string)
  140. {
  141. $slugify = new Slugify();
  142. return $slugify->slugify($string);
  143. }
  144. function camelCase($str)
  145. {
  146. $i = array("-", "_");
  147. $str = preg_replace('/([a-z])([A-Z])/', "\\1 \\2", $str);
  148. $str = preg_replace('@[^a-zA-Z0-9\-_ ]+@', '', $str);
  149. $str = str_replace($i, ' ', $str);
  150. $str = str_replace(' ', '', ucwords(strtolower($str)));
  151. $str = strtolower(substr($str, 0, 1)) . substr($str, 1);
  152. return $str;
  153. }
  154. function snakeCase($str)
  155. {
  156. $str = preg_replace('/([a-z])([A-Z])/', "\\1_\\2", $str);
  157. $str = strtolower($str);
  158. return $str;
  159. }
  160. public function csvEscape($str)
  161. {
  162. return str_replace(array("\r", "\n"), ' ', $str);
  163. }
  164. public function urlEncryptData($datas){
  165. $key = 'secretToken';
  166. foreach ($datas as $data) {
  167. $key .= $data;
  168. }
  169. return md5($key);
  170. }
  171. public function formatPhoneNumber($phone)
  172. {
  173. $phone = preg_replace('`[^0-9]`', '', $phone);
  174. if(strlen($phone) == 10) {
  175. $phone = '+33'.substr($phone, 1, 9) ;
  176. }
  177. elseif(strlen($phone) == 11 && substr($phone, 0, 2) == '33') {
  178. $phone = '+'.$phone ;
  179. }
  180. return $phone ;
  181. }
  182. }