utf.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /*
  3. Copyright (c) 2009-2019 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. //! Unicode string manager
  16. class UTF extends Prefab {
  17. /**
  18. * Get string length
  19. * @return int
  20. * @param $str string
  21. **/
  22. function strlen($str) {
  23. preg_match_all('/./us',$str,$parts);
  24. return count($parts[0]);
  25. }
  26. /**
  27. * Reverse a string
  28. * @return string
  29. * @param $str string
  30. **/
  31. function strrev($str) {
  32. preg_match_all('/./us',$str,$parts);
  33. return implode('',array_reverse($parts[0]));
  34. }
  35. /**
  36. * Find position of first occurrence of a string (case-insensitive)
  37. * @return int|FALSE
  38. * @param $stack string
  39. * @param $needle string
  40. * @param $ofs int
  41. **/
  42. function stripos($stack,$needle,$ofs=0) {
  43. return $this->strpos($stack,$needle,$ofs,TRUE);
  44. }
  45. /**
  46. * Find position of first occurrence of a string
  47. * @return int|FALSE
  48. * @param $stack string
  49. * @param $needle string
  50. * @param $ofs int
  51. * @param $case bool
  52. **/
  53. function strpos($stack,$needle,$ofs=0,$case=FALSE) {
  54. return preg_match('/^(.{'.$ofs.'}.*?)'.
  55. preg_quote($needle,'/').'/us'.($case?'i':''),$stack,$match)?
  56. $this->strlen($match[1]):FALSE;
  57. }
  58. /**
  59. * Returns part of haystack string from the first occurrence of
  60. * needle to the end of haystack (case-insensitive)
  61. * @return string|FALSE
  62. * @param $stack string
  63. * @param $needle string
  64. * @param $before bool
  65. **/
  66. function stristr($stack,$needle,$before=FALSE) {
  67. return $this->strstr($stack,$needle,$before,TRUE);
  68. }
  69. /**
  70. * Returns part of haystack string from the first occurrence of
  71. * needle to the end of haystack
  72. * @return string|FALSE
  73. * @param $stack string
  74. * @param $needle string
  75. * @param $before bool
  76. * @param $case bool
  77. **/
  78. function strstr($stack,$needle,$before=FALSE,$case=FALSE) {
  79. if (!$needle)
  80. return FALSE;
  81. preg_match('/^(.*?)'.preg_quote($needle,'/').'/us'.($case?'i':''),
  82. $stack,$match);
  83. return isset($match[1])?
  84. ($before?
  85. $match[1]:
  86. $this->substr($stack,$this->strlen($match[1]))):
  87. FALSE;
  88. }
  89. /**
  90. * Return part of a string
  91. * @return string|FALSE
  92. * @param $str string
  93. * @param $start int
  94. * @param $len int
  95. **/
  96. function substr($str,$start,$len=0) {
  97. if ($start<0)
  98. $start=$this->strlen($str)+$start;
  99. if (!$len)
  100. $len=$this->strlen($str)-$start;
  101. return preg_match('/^.{'.$start.'}(.{0,'.$len.'})/us',$str,$match)?
  102. $match[1]:FALSE;
  103. }
  104. /**
  105. * Count the number of substring occurrences
  106. * @return int
  107. * @param $stack string
  108. * @param $needle string
  109. **/
  110. function substr_count($stack,$needle) {
  111. preg_match_all('/'.preg_quote($needle,'/').'/us',$stack,
  112. $matches,PREG_SET_ORDER);
  113. return count($matches);
  114. }
  115. /**
  116. * Strip whitespaces from the beginning of a string
  117. * @return string
  118. * @param $str string
  119. **/
  120. function ltrim($str) {
  121. return preg_replace('/^[\pZ\pC]+/u','',$str);
  122. }
  123. /**
  124. * Strip whitespaces from the end of a string
  125. * @return string
  126. * @param $str string
  127. **/
  128. function rtrim($str) {
  129. return preg_replace('/[\pZ\pC]+$/u','',$str);
  130. }
  131. /**
  132. * Strip whitespaces from the beginning and end of a string
  133. * @return string
  134. * @param $str string
  135. **/
  136. function trim($str) {
  137. return preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u','',$str);
  138. }
  139. /**
  140. * Return UTF-8 byte order mark
  141. * @return string
  142. **/
  143. function bom() {
  144. return chr(0xef).chr(0xbb).chr(0xbf);
  145. }
  146. /**
  147. * Convert code points to Unicode symbols
  148. * @return string
  149. * @param $str string
  150. **/
  151. function translate($str) {
  152. return html_entity_decode(
  153. preg_replace('/\\\\u([[:xdigit:]]+)/i','&#x\1;',$str));
  154. }
  155. /**
  156. * Translate emoji tokens to Unicode font-supported symbols
  157. * @return string
  158. * @param $str string
  159. **/
  160. function emojify($str) {
  161. $map=[
  162. ':('=>'\u2639', // frown
  163. ':)'=>'\u263a', // smile
  164. '<3'=>'\u2665', // heart
  165. ':D'=>'\u1f603', // grin
  166. 'XD'=>'\u1f606', // laugh
  167. ';)'=>'\u1f609', // wink
  168. ':P'=>'\u1f60b', // tongue
  169. ':,'=>'\u1f60f', // think
  170. ':/'=>'\u1f623', // skeptic
  171. '8O'=>'\u1f632', // oops
  172. ]+Base::instance()->EMOJI;
  173. return $this->translate(str_replace(array_keys($map),
  174. array_values($map),$str));
  175. }
  176. }