1<?php
2
3/**
4 * Translates a character position into an 'absolute' byte position.
5 * Unit tested by Kasper.
6 * (http://phpxref.com/xref/moodle/lib/typo3/class.t3lib_cs.php.source.html.gz)
7 *
8 * @param    string        UTF-8 string
9 * @param    integer        Character position (negative values start from the end)
10 * @return    integer        Byte position
11 * @author    Martin Kutschker <martin.t.kutschker@blackbox.net>
12 */
13if (!function_exists('utf8_char2byte_pos')) {
14  function utf8_char2byte_pos($str,$pos)    {
15    $n = 0;                // number of characters found
16    $p = abs($pos);        // number of characters wanted
17
18    if ($pos >= 0)    {
19      $i = 0;
20      $d = 1;
21    } else {
22      $i = strlen($str)-1;
23      $d = -1;
24    }
25
26    for( ; strlen($str{$i}) && $n<$p; $i+=$d)    {
27      $c = (int)ord($str{$i});
28      if (!($c & 0x80))    // single-byte (0xxxxxx)
29        $n++;
30      elseif (($c & 0xC0) == 0xC0)    // multi-byte starting byte (11xxxxxx)
31        $n++;
32    }
33    if (!strlen($str{$i}))    return false; // offset beyond string length
34
35    if ($pos >= 0)    {
36      // skip trailing multi-byte data bytes
37      while ((ord($str{$i}) & 0x80) && !(ord($str{$i}) & 0x40)) { $i++; }
38    } else {
39      // correct offset
40      $i++;
41    }
42
43    return $i;
44  }
45}
46
47?>