3 * Locate a byte index given a UTF-8 character index
4 * @version $Id: position.php,v 1.1 2006/10/01 00:01:31 harryf Exp $
9 //--------------------------------------------------------------------
11 * Given a string and a character index in the string, in
12 * terms of the UTF-8 character position, returns the byte
13 * index of that character. Can be useful when you want to
14 * PHP's native string functions but we warned, locating
15 * the byte can be expensive
16 * Takes variable number of parameters - first must be
17 * the search string then 1 to n UTF-8 character positions
18 * to obtain byte indexes for - it is more efficient to search
19 * the string for multiple characters at once, than make
20 * repeated calls to this function
22 * @author Chris Smith<chris@jalakai.co.uk>
23 * @param string string to locate index in
24 * @param int (n times)
25 * @return mixed - int if only one input int, array if more
26 * @return boolean TRUE if it's all ASCII
28 * @subpackage position
30 function utf8_byte_position() {
32 $args = func_get_args();
33 $str =& array_shift($args);
34 if (!is_string($str)) return false;
38 // trivial byte index, character offset pair
41 // use a short piece of str to estimate bytes per character
42 // $i (& $j) -> byte indexes into $str
43 $i = utf8_locate_next_chr($str, 300);
45 // $c -> character offset into $str
46 $c = strlen(utf8_decode(substr($str,0,$i)));
48 // deal with arguments from lowest to highest
51 foreach ($args as $offset) {
52 // sanity checks FIXME
55 if ($offset == 0) { $result[] = 0; continue; }
57 // ensure no endless looping
62 if ( ($c - $prev[1]) == 0 ) {
63 // Hack: gone past end of string
69 $j = $i + (int)(($offset-$c) * ($i - $prev[0]) / ($c - $prev[1]));
71 // correct to utf8 character boundary
72 $j = utf8_locate_next_chr($str, $j);
74 // save the index, offset for use next iteration
78 // determine new character offset
79 $c += strlen(utf8_decode(substr($str,$i,$j-$i)));
82 $c -= strlen(utf8_decode(substr($str,$j,$i-$j)));
85 $error = abs($c-$offset);
87 // ready for next time around
90 // from 7 it is faster to iterate over the string
91 } while ( ($error > 7) && --$safety_valve) ;
93 if ($error && $error <= 7) {
97 while ($error--) { $i = utf8_locate_next_chr($str,++$i); }
100 while ($error--) { $i = utf8_locate_current_chr($str,--$i); }
103 // ready for next arg
109 if ( count($result) == 1 ) {
116 //--------------------------------------------------------------------
118 * Given a string and any byte index, returns the byte index
119 * of the start of the current UTF-8 character, relative to supplied
120 * position. If the current character begins at the same place as the
121 * supplied byte index, that byte index will be returned. Otherwise
122 * this function will step backwards, looking for the index where
123 * curent UTF-8 character begins
124 * @author Chris Smith<chris@jalakai.co.uk>
126 * @param int byte index in the string
127 * @return int byte index of start of next UTF-8 character
129 * @subpackage position
131 function utf8_locate_current_chr( &$str, $idx ) {
133 if ($idx <= 0) return 0;
135 $limit = strlen($str);
136 if ($idx >= $limit) return $limit;
138 // Binary value for any byte after the first in a multi-byte UTF-8 character
139 // will be like 10xxxxxx so & 0xC0 can be used to detect this kind
140 // of byte - assuming well formed UTF-8
141 while ($idx && ((ord($str[$idx]) & 0xC0) == 0x80)) $idx--;
146 //--------------------------------------------------------------------
148 * Given a string and any byte index, returns the byte index
149 * of the start of the next UTF-8 character, relative to supplied
150 * position. If the next character begins at the same place as the
151 * supplied byte index, that byte index will be returned.
152 * @author Chris Smith<chris@jalakai.co.uk>
154 * @param int byte index in the string
155 * @return int byte index of start of next UTF-8 character
157 * @subpackage position
159 function utf8_locate_next_chr( &$str, $idx ) {
161 if ($idx <= 0) return 0;
163 $limit = strlen($str);
164 if ($idx >= $limit) return $limit;
166 // Binary value for any byte after the first in a multi-byte UTF-8 character
167 // will be like 10xxxxxx so & 0xC0 can be used to detect this kind
168 // of byte - assuming well formed UTF-8
169 while (($idx < $limit) && ((ord($str[$idx]) & 0xC0) == 0x80)) $idx++;