[Phpfreechat-svn] SF.net SVN: phpfreechat: [1034] trunk/lib/utf8
Status: Beta
Brought to you by:
kerphi
|
From: <ke...@us...> - 2007-06-11 08:19:15
|
Revision: 1034
http://svn.sourceforge.net/phpfreechat/?rev=1034&view=rev
Author: kerphi
Date: 2007-06-11 01:19:13 -0700 (Mon, 11 Jun 2007)
Log Message:
-----------
Fix a problem with phpbb3 integration (thanks to Larry) http://www.phpfreechat.net/forum/viewtopic.php?id=1576
Modified Paths:
--------------
trunk/lib/utf8/utf8_char2byte_pos.php
trunk/lib/utf8/utf8_strlen.php
trunk/lib/utf8/utf8_substr.php
Modified: trunk/lib/utf8/utf8_char2byte_pos.php
===================================================================
--- trunk/lib/utf8/utf8_char2byte_pos.php 2007-06-07 12:24:53 UTC (rev 1033)
+++ trunk/lib/utf8/utf8_char2byte_pos.php 2007-06-11 08:19:13 UTC (rev 1034)
@@ -10,36 +10,38 @@
* @return integer Byte position
* @author Martin Kutschker <mar...@bl...>
*/
-function utf8_char2byte_pos($str,$pos) {
- $n = 0; // number of characters found
- $p = abs($pos); // number of characters wanted
+if (!function_exists('utf8_char2byte_pos')) {
+ function utf8_char2byte_pos($str,$pos) {
+ $n = 0; // number of characters found
+ $p = abs($pos); // number of characters wanted
- if ($pos >= 0) {
- $i = 0;
- $d = 1;
- } else {
- $i = strlen($str)-1;
- $d = -1;
- }
+ if ($pos >= 0) {
+ $i = 0;
+ $d = 1;
+ } else {
+ $i = strlen($str)-1;
+ $d = -1;
+ }
- for( ; strlen($str{$i}) && $n<$p; $i+=$d) {
- $c = (int)ord($str{$i});
- if (!($c & 0x80)) // single-byte (0xxxxxx)
- $n++;
- elseif (($c & 0xC0) == 0xC0) // multi-byte starting byte (11xxxxxx)
- $n++;
- }
- if (!strlen($str{$i})) return false; // offset beyond string length
+ for( ; strlen($str{$i}) && $n<$p; $i+=$d) {
+ $c = (int)ord($str{$i});
+ if (!($c & 0x80)) // single-byte (0xxxxxx)
+ $n++;
+ elseif (($c & 0xC0) == 0xC0) // multi-byte starting byte (11xxxxxx)
+ $n++;
+ }
+ if (!strlen($str{$i})) return false; // offset beyond string length
- if ($pos >= 0) {
- // skip trailing multi-byte data bytes
- while ((ord($str{$i}) & 0x80) && !(ord($str{$i}) & 0x40)) { $i++; }
- } else {
- // correct offset
- $i++;
+ if ($pos >= 0) {
+ // skip trailing multi-byte data bytes
+ while ((ord($str{$i}) & 0x80) && !(ord($str{$i}) & 0x40)) { $i++; }
+ } else {
+ // correct offset
+ $i++;
+ }
+
+ return $i;
}
-
- return $i;
}
?>
\ No newline at end of file
Modified: trunk/lib/utf8/utf8_strlen.php
===================================================================
--- trunk/lib/utf8/utf8_strlen.php 2007-06-07 12:24:53 UTC (rev 1033)
+++ trunk/lib/utf8/utf8_strlen.php 2007-06-11 08:19:13 UTC (rev 1034)
@@ -9,16 +9,18 @@
* @see strlen()
* @author Martin Kutschker <mar...@bl...>
*/
-function utf8_strlen($str) {
- $n=0;
- for($i=0; isset($str{$i}) && strlen($str{$i})>0; $i++) {
- $c = ord($str{$i});
- if (!($c & 0x80)) // single-byte (0xxxxxx)
- $n++;
- elseif (($c & 0xC0) == 0xC0) // multi-byte starting byte (11xxxxxx)
- $n++;
+if (!function_exists('utf8_strlen')) {
+ function utf8_strlen($str) {
+ $n=0;
+ for($i=0; isset($str{$i}) && strlen($str{$i})>0; $i++) {
+ $c = ord($str{$i});
+ if (!($c & 0x80)) // single-byte (0xxxxxx)
+ $n++;
+ elseif (($c & 0xC0) == 0xC0) // multi-byte starting byte (11xxxxxx)
+ $n++;
+ }
+ return $n;
}
- return $n;
}
?>
\ No newline at end of file
Modified: trunk/lib/utf8/utf8_substr.php
===================================================================
--- trunk/lib/utf8/utf8_substr.php 2007-06-07 12:24:53 UTC (rev 1033)
+++ trunk/lib/utf8/utf8_substr.php 2007-06-11 08:19:13 UTC (rev 1034)
@@ -14,28 +14,30 @@
* @see substr()
* @author Martin Kutschker <mar...@bl...>
*/
-function utf8_substr($str,$start,$len=null) {
- if (!strcmp($len,'0')) return '';
+if (!function_exists('utf8_substr')) {
+ function utf8_substr($str,$start,$len=null) {
+ if (!strcmp($len,'0')) return '';
- $byte_start = @utf8_char2byte_pos($str,$start);
- if ($byte_start === false) {
- if ($start > 0) {
- return false; // $start outside string length
- } else {
- $start = 0;
+ $byte_start = @utf8_char2byte_pos($str,$start);
+ if ($byte_start === false) {
+ if ($start > 0) {
+ return false; // $start outside string length
+ } else {
+ $start = 0;
+ }
}
- }
- $str = substr($str,$byte_start);
+ $str = substr($str,$byte_start);
- if ($len!=null) {
- $byte_end = @utf8_char2byte_pos($str,$len);
- if ($byte_end === false) // $len outside actual string length
- return $len<0 ? '' : $str; // When length is less than zero and exceeds, then we return blank string.
- else
- return substr($str,0,$byte_end);
+ if ($len!=null) {
+ $byte_end = @utf8_char2byte_pos($str,$len);
+ if ($byte_end === false) // $len outside actual string length
+ return $len<0 ? '' : $str; // When length is less than zero and exceeds, then we return blank string.
+ else
+ return substr($str,0,$byte_end);
+ }
+ else return $str;
}
- else return $str;
}
?>
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|