Просмотр файла news/libraries/utf.php

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