Mbstring.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\g11n\multibyte\adapter;
  9. /**
  10. * The `Mbstring` class is an adapter which uses certain string functions from
  11. * `ext/mbstring`. You will need to have the extension installed to use this adapter.
  12. *
  13. * No known limitations affecting used functionality. Silently strips
  14. * out badly formed UTF-8 sequences.
  15. *
  16. * @link http://php.net/manual/en/book.mbstring.php
  17. */
  18. class Mbstring extends \lithium\core\Object {
  19. /**
  20. * Determines if this adapter is enabled by checking if the `mbstring` extension is loaded.
  21. *
  22. * @return boolean Returns `true` if enabled, otherwise `false`.
  23. */
  24. public static function enabled() {
  25. return extension_loaded('mbstring');
  26. }
  27. /**
  28. * Here used as a multibyte enabled equivalent of `strlen()`.
  29. *
  30. * @link http://php.net/manual/en/function.mb-strlen.php
  31. * @param string $string
  32. * @return integer
  33. */
  34. public function strlen($string) {
  35. return mb_strlen($string, 'UTF-8');
  36. }
  37. /**
  38. * Here used as a multibyte enabled equivalent of `strpos()`.
  39. *
  40. * @link http://php.net/manual/en/function.mb-strpos.php
  41. * @param string $haystack
  42. * @param string $needle
  43. * @param integer $offset
  44. * @return integer|boolean
  45. */
  46. public function strpos($haystack, $needle, $offset) {
  47. return mb_strpos($haystack, $needle, $offset, 'UTF-8');
  48. }
  49. /**
  50. * Here used as a multibyte enabled equivalent of `strrpos()`.
  51. *
  52. * @link http://php.net/manual/en/function.mb-strpos.php
  53. * @param string $haystack
  54. * @param string $needle
  55. * @return integer|boolean
  56. */
  57. public function strrpos($haystack, $needle) {
  58. return mb_strrpos($haystack, $needle, 0, 'UTF-8');
  59. }
  60. /**
  61. * Here used as a multibyte enabled equivalent of `substr()`.
  62. *
  63. * @link http://php.net/manual/en/function.mb-substr.php
  64. * @param string $string
  65. * @param integer $start
  66. * @param integer $length
  67. * @return string|boolean
  68. */
  69. public function substr($string, $start, $length) {
  70. return mb_substr($string, $start, $length, 'UTF-8');
  71. }
  72. }
  73. ?>