/libraries/phputf8/mbstring/core.php

https://github.com/shafiqissani/Jewelery-Ecommerce- · PHP · 90 lines · 38 code · 7 blank · 45 comment · 13 complexity · 1408c59025bac52b1cf185e82148ed3e MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: core.php 10381 2008-06-01 03:35:53Z pasamio $
  4. * @package utf8
  5. * @subpackage strings
  6. */
  7. /**
  8. * Define UTF8_CORE as required
  9. */
  10. if ( !defined('UTF8_CORE') ) {
  11. define('UTF8_CORE',TRUE);
  12. }
  13. //--------------------------------------------------------------------
  14. /**
  15. * Assumes mbstring internal encoding is set to UTF-8
  16. * Wrapper around mb_strpos
  17. * Find position of first occurrence of a string
  18. * @param string haystack
  19. * @param string needle (you should validate this with utf8_is_valid)
  20. * @param integer offset in characters (from left)
  21. * @return mixed integer position or FALSE on failure
  22. * @package utf8
  23. * @subpackage strings
  24. */
  25. function utf8_strpos($str, $search, $offset = FALSE){
  26. if(strlen($str) && strlen($search)) {
  27. if ( $offset === FALSE ) {
  28. return mb_strpos($str, $search);
  29. } else {
  30. return mb_strpos($str, $search, $offset);
  31. }
  32. } else return FALSE;
  33. }
  34. //--------------------------------------------------------------------
  35. /**
  36. * Assumes mbstring internal encoding is set to UTF-8
  37. * Wrapper around mb_strrpos
  38. * Find position of last occurrence of a char in a string
  39. * @param string haystack
  40. * @param string needle (you should validate this with utf8_is_valid)
  41. * @param integer (optional) offset (from left)
  42. * @return mixed integer position or FALSE on failure
  43. * @package utf8
  44. * @subpackage strings
  45. */
  46. function utf8_strrpos($str, $search, $offset = FALSE){
  47. if ( $offset === FALSE ) {
  48. # Emulate behaviour of strrpos rather than raising warning
  49. if ( empty($str) ) {
  50. return FALSE;
  51. }
  52. return mb_strrpos($str, $search);
  53. } else {
  54. if ( !is_int($offset) ) {
  55. trigger_error('utf8_strrpos expects parameter 3 to be long',E_USER_WARNING);
  56. return FALSE;
  57. }
  58. $str = mb_substr($str, $offset);
  59. if ( FALSE !== ( $pos = mb_strrpos($str, $search) ) ) {
  60. return $pos + $offset;
  61. }
  62. return FALSE;
  63. }
  64. }
  65. //--------------------------------------------------------------------
  66. /**
  67. * Assumes mbstring internal encoding is set to UTF-8
  68. * Wrapper around mb_substr
  69. * Return part of a string given character offset (and optionally length)
  70. * @param string
  71. * @param integer number of UTF-8 characters offset (from left)
  72. * @param integer (optional) length in UTF-8 characters from offset
  73. * @return mixed string or FALSE if failure
  74. * @package utf8
  75. * @subpackage strings
  76. */
  77. function utf8_substr($str, $offset, $length = FALSE){
  78. if ( $length === FALSE ) {
  79. return mb_substr($str, $offset);
  80. } else {
  81. return mb_substr($str, $offset, $length);
  82. }
  83. }