PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/01.Source/01.CORE/includes/utf8/php_string_handler.php

http://creative-portal.googlecode.com/
PHP | 176 lines | 92 code | 23 blank | 61 comment | 12 complexity | 6c842cdd46b5680d497c4dcd5112aa4e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @Project NUKEVIET 3.0
  4. * @Author VINADES.,JSC (contact@vinades.vn)
  5. * @Copyright (C) 2010 VINADES.,JSC. All rights reserved
  6. * @Createdate 22/8/2010, 19:33
  7. */
  8. if ( ! defined( 'NV_MAINFILE' ) ) die( 'Stop!!!' );
  9. /**
  10. * nv_internal_encoding()
  11. *
  12. * @param mixed $encoding
  13. * @return
  14. */
  15. function nv_internal_encoding ( $encoding )
  16. {
  17. return false;
  18. }
  19. /**
  20. * nv_strlen()
  21. *
  22. * @param mixed $string
  23. * @return
  24. */
  25. function nv_strlen( $string )
  26. {
  27. return preg_match_all( '/./u', $string, $tmp );
  28. }
  29. /**
  30. * nv_substr()
  31. *
  32. * @param mixed $string
  33. * @param mixed $start
  34. * @param mixed $length
  35. * @return
  36. */
  37. function nv_substr( $string, $start, $length )
  38. {
  39. $nv_strlen = nv_strlen( $string );
  40. if ( $start < 0 ) $start = $nv_strlen + $start;
  41. if ( $length < 0 ) $length = $nv_strlen - $start + $length;
  42. $xlen = $nv_strlen - $start;
  43. $length = ( $length > $xlen ) ? $xlen : $length;
  44. preg_match( '/^.{' . $start . '}(.{0,' . $length . '})/us', $string, $tmp );
  45. return ( isset( $tmp[1] ) ) ? $tmp[1] : false;
  46. }
  47. /**
  48. * nv_substr_count()
  49. *
  50. * @param mixed $haystack
  51. * @param mixed $needle
  52. * @return
  53. */
  54. function nv_substr_count( $haystack, $needle )
  55. {
  56. $needle = preg_quote( $needle, '/' );
  57. preg_match_all( '/' . $needle . '/u', $haystack, $dummy );
  58. return count( $dummy[0] );
  59. }
  60. /**
  61. * nv2_strpos()
  62. *
  63. * @param mixed $haystack
  64. * @param mixed $needle
  65. * @param integer $offset
  66. * @return
  67. */
  68. function nv_strpos( $haystack, $needle, $offset = 0 )
  69. {
  70. $offset = ( $offset < 0 ) ? 0 : $offset;
  71. if ( $offset > 0 )
  72. {
  73. preg_match( '/^.{' . $offset . '}(.*)/us', $haystack, $dummy );
  74. $haystack = ( isset( $dummy[1] ) ) ? $dummy[1] : '';
  75. }
  76. $p = strpos( $haystack, $needle );
  77. if ( $haystack == '' or $p === false ) return false;
  78. $r = $offset;
  79. $i = 0;
  80. while ( $i < $p )
  81. {
  82. if ( ord( $haystack[$i] ) < 128 )
  83. {
  84. $i = $i + 1;
  85. }
  86. else
  87. {
  88. $bvalue = decbin( ord( $haystack[$i] ) );
  89. $i = $i + strlen( preg_replace( '/^(1+)(.+)$/', '\1', $bvalue ) );
  90. }
  91. $r++;
  92. }
  93. return $r;
  94. }
  95. /**
  96. * nv_strrpos()
  97. *
  98. * @param mixed $haystack
  99. * @param mixed $needle
  100. * @param mixed $offset
  101. * @return
  102. */
  103. function nv_strrpos( $haystack, $needle, $offset = null )
  104. {
  105. if ( is_null( $offset ) )
  106. {
  107. $ar = explode( $needle, $haystack );
  108. if ( count( $ar ) > 1 )
  109. {
  110. array_pop( $ar );
  111. $haystack = join( $needle, $ar );
  112. return nv_strlen( $haystack );
  113. }
  114. return false;
  115. }
  116. else
  117. {
  118. if ( ! is_int( $offset ) )
  119. {
  120. trigger_error( 'nv_strrpos expects parameter 3 to be long', E_USER_WARNING );
  121. return false;
  122. }
  123. $haystack = nv_substr( $haystack, $offset );
  124. if ( false !== ( $pos = nv_strrpos( $haystack, $needle ) ) )
  125. {
  126. return $pos + $offset;
  127. }
  128. return false;
  129. }
  130. }
  131. /**
  132. * nv_strtolower()
  133. *
  134. * @param mixed $string
  135. * @return
  136. */
  137. function nv_strtolower( $string )
  138. {
  139. include ( NV_ROOTDIR . '/includes/utf8/lookup.php' );
  140. return strtr( $string, $utf8_lookup['strtolower'] );
  141. }
  142. /**
  143. * nv_strtoupper()
  144. *
  145. * @param mixed $string
  146. * @return
  147. */
  148. function nv_strtoupper( $string )
  149. {
  150. include ( NV_ROOTDIR . '/includes/utf8/lookup.php' );
  151. return strtr( $string, $utf8_lookup['strtoupper'] );
  152. }
  153. ?>