utf.php 4.3 KB

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