]> git.wh0rd.org - tt-rss.git/blame - compat.php
update_daemon2: make proper lockfile
[tt-rss.git] / compat.php
CommitLineData
1d3a17c7 1<?php
5bc0bd27
AD
2
3if (!function_exists('mb_strlen'))
4{
5 // from Typo3
6 // author Martin Kutschker <martin.t.kutschker@blackbox.net>
7 function mb_strlen($str, $encoding="utf-8")
8 {
9 if($encoding != "utf-8")
10 { return -1; }
11
12 $n=0;
13
14 for($i=0; isset($str{$i}); $i++)
15 {
16 $c = ord($str{$i});
17
18 if (!($c & 0x80)) // single-byte (0xxxxxx)
19 $n++;
20
21 elseif (($c & 0xC0) == 0xC0) // multi-byte starting byte (11xxxxxx)
22 $n++;
23 }
24
25 return $n;
26 }
27}
28
29if (!function_exists('mb_substr'))
30{
31 // from Typo3
32 // author Martin Kutschker <martin.t.kutschker@blackbox.net>
33 function mb_substr($str, $start, $len=null, $encoding="utf-8")
34 {
35 if($encoding != "utf-8")
36 { return -1; }
37
38 $byte_start = utf8_char2byte_pos($str,$start);
39
40 if ($byte_start === false)
41 return false; // $start outside string length
42
43 $str = substr($str,$byte_start);
44
45 if ($len != null)
46 {
47 $byte_end = utf8_char2byte_pos($str,$len);
48
49 if ($byte_end === false) // $len outside actual string length
50 return $str;
51 else
52 return substr($str,0,$byte_end);
53 }
54
55 else return $str;
56 }
57
58 function utf8_char2byte_pos($str,$pos)
59 {
60 $n = 0; // number of characters found
61 $p = abs($pos); // number of characters wanted
62
63 if ($pos >= 0)
64 {
65 $i = 0;
66 $d = 1;
67 } else {
68 $i = strlen($str)-1;
69 $d = -1;
70 }
71
72 for( ; isset($str{$i}) && $n<$p; $i+=$d)
73 {
74 $c = (int)ord($str{$i});
75
76 if (!($c & 0x80)) // single-byte (0xxxxxx)
77 $n++;
78 elseif (($c & 0xC0) == 0xC0) // multi-byte starting byte (11xxxxxx)
79 $n++;
80 }
81
82 if (!isset($str{$i}))
83 return false; // offset beyond string length
84
85 if ($pos >= 0)
86 {
87 // skip trailing multi-byte data bytes
88 while ((ord($str{$i}) & 0x80) && !(ord($str{$i}) & 0x40))
89 { $i++; }
90 } else {
91 // correct offset
92 $i++;
93 }
94
95 return $i;
de5b8ebc
AD
96 }
97
98 if (!function_exists('mb_strtolower')) {
99 function mb_strtolower($msg, $encoding) {
100 return $msg;
101 }
102 }
5bc0bd27 103}