3 * @version $Id: core.php,v 1.5 2006/02/28 22:12:25 harryf Exp $
9 * Define UTF8_CORE as required
11 if ( !defined('UTF8_CORE') ) {
12 define('UTF8_CORE',TRUE);
15 //--------------------------------------------------------------------
17 * Wrapper round mb_strlen
18 * Assumes you have mb_internal_encoding to UTF-8 already
19 * Note: this function does not count bad bytes in the string - these
21 * @param string UTF-8 string
22 * @return int number of UTF-8 characters in string
26 function utf8_strlen($str){
27 return mb_strlen($str);
31 //--------------------------------------------------------------------
33 * Assumes mbstring internal encoding is set to UTF-8
34 * Wrapper around mb_strpos
35 * Find position of first occurrence of a string
36 * @param string haystack
37 * @param string needle (you should validate this with utf8_is_valid)
38 * @param integer offset in characters (from left)
39 * @return mixed integer position or FALSE on failure
43 function utf8_strpos($str, $search, $offset = FALSE){
44 if ( $offset === FALSE ) {
45 return mb_strpos($str, $search);
47 return mb_strpos($str, $search, $offset);
51 //--------------------------------------------------------------------
53 * Assumes mbstring internal encoding is set to UTF-8
54 * Wrapper around mb_strrpos
55 * Find position of last occurrence of a char in a string
56 * @param string haystack
57 * @param string needle (you should validate this with utf8_is_valid)
58 * @param integer (optional) offset (from left)
59 * @return mixed integer position or FALSE on failure
63 function utf8_strrpos($str, $search, $offset = FALSE){
64 if ( $offset === FALSE ) {
65 # Emulate behaviour of strrpos rather than raising warning
69 return mb_strrpos($str, $search);
71 if ( !is_int($offset) ) {
72 trigger_error('utf8_strrpos expects parameter 3 to be long',E_USER_WARNING);
76 $str = mb_substr($str, $offset);
78 if ( FALSE !== ( $pos = mb_strrpos($str, $search) ) ) {
79 return $pos + $offset;
86 //--------------------------------------------------------------------
88 * Assumes mbstring internal encoding is set to UTF-8
89 * Wrapper around mb_substr
90 * Return part of a string given character offset (and optionally length)
92 * @param integer number of UTF-8 characters offset (from left)
93 * @param integer (optional) length in UTF-8 characters from offset
94 * @return mixed string or FALSE if failure
98 function utf8_substr($str, $offset, $length = FALSE){
99 if ( $length === FALSE ) {
100 return mb_substr($str, $offset);
102 return mb_substr($str, $offset, $length);
106 //--------------------------------------------------------------------
108 * Assumes mbstring internal encoding is set to UTF-8
109 * Wrapper around mb_strtolower
110 * Make a string lowercase
111 * Note: The concept of a characters "case" only exists is some alphabets
112 * such as Latin, Greek, Cyrillic, Armenian and archaic Georgian - it does
113 * not exist in the Chinese alphabet, for example. See Unicode Standard
114 * Annex #21: Case Mappings
116 * @return mixed either string in lowercase or FALSE is UTF-8 invalid
118 * @subpackage strings
120 function utf8_strtolower($str){
121 return mb_strtolower($str);
124 //--------------------------------------------------------------------
126 * Assumes mbstring internal encoding is set to UTF-8
127 * Wrapper around mb_strtoupper
128 * Make a string uppercase
129 * Note: The concept of a characters "case" only exists is some alphabets
130 * such as Latin, Greek, Cyrillic, Armenian and archaic Georgian - it does
131 * not exist in the Chinese alphabet, for example. See Unicode Standard
132 * Annex #21: Case Mappings
134 * @return mixed either string in lowercase or FALSE is UTF-8 invalid
136 * @subpackage strings
138 function utf8_strtoupper($str){
139 return mb_strtoupper($str);