Skip to content
Browse files

MDL-39335: add an ord() unicode analog to textlib.

  • Loading branch information...
1 parent cf5a329 commit 1977123ea571c2eb2d5661beb57cffdba626ad01 @vostreltsov vostreltsov committed Apr 24, 2013
Showing with 42 additions and 0 deletions.
  1. +13 −0 lib/tests/textlib_test.php
  2. +29 −0 lib/textlib.class.php
View
13 lib/tests/textlib_test.php
@@ -341,6 +341,19 @@ public function test_code2utf8() {
}
/**
+ * Tests the static utf8ord method
+ * @return void
+ */
+ public function test_utf8ord() {
+ $this->assertSame(textlib::utf8ord(''), ord(''));
+ $this->assertSame(textlib::utf8ord('f'), ord('f'));
+ $this->assertSame(textlib::utf8ord('α'), 0x03B1);
+ $this->assertSame(textlib::utf8ord('й'), 0x0439);
+ $this->assertSame(textlib::utf8ord('𯨟'), 0x2FA1F);
+ $this->assertSame(textlib::utf8ord('Ž'), 381);
+ }
+
+ /**
* Tests the static strtotitle method
* @return void
*/
View
29 lib/textlib.class.php
@@ -592,6 +592,35 @@ public static function code2utf8($num) {
}
/**
+ * Returns the code of the given UTF-8 character
+ *
+ * @param string $utf8char one UTF-8 character
+ * @return int the code of the given character
+ */
+ public static function utf8ord($utf8char) {
+ if ($utf8char == '') {
+ return 0;
+ }
+ $ord0 = ord($utf8char{0});
+ if ($ord0 >= 0 && $ord0 <= 127) {
+ return $ord0;
+ }
+ $ord1 = ord($utf8char{1});
+ if ($ord0 >= 192 && $ord0 <= 223) {
+ return ($ord0 - 192) * 64 + ($ord1 - 128);
+ }
+ $ord2 = ord($utf8char{2});
+ if ($ord0 >= 224 && $ord0 <= 239) {
+ return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
+ }
+ $ord3 = ord($utf8char{3});
+ if ($ord0 >= 240 && $ord0 <= 247) {
+ return ($ord0 - 240) * 262144 + ($ord1 - 128 )* 4096 + ($ord2 - 128) * 64 + ($ord3 - 128);
+ }
+ return false;
+ }
+
+ /**
* Makes first letter of each word capital - words must be separated by spaces.
* Use with care, this function does not work properly in many locales!!!
*

0 comments on commit 1977123

Please sign in to comment.
Something went wrong with that request. Please try again.