List Info

Thread: note 78032 added to function.ord




note 78032 added to function.ord
user name
2007-09-24 22:04:15
Here's my take on an earlier-posted UTF-8 version of ord,
suitable for iterating through a string by Unicode value.
The function can optionally take an index into a string, and
optionally return the number of bytes consumed by a
character so that you know how much to increment the index
to get to the next character.

<?php

function ordUTF8($c, $index = 0, &$bytes = null)
{
  $len = strlen($c);
  $bytes = 0;

  if ($index >= $len)
    return false;

  $h = ord($c{$index});

  if ($h <= 0x7F) {
    $bytes = 1;
    return $h;
  }
  else if ($h < 0xC2)
    return false;
  else if ($h <= 0xDF && $index < $len - 1) {
    $bytes = 2;
    return ($h & 0x1F) <<  6 | (ord($c{$index +
1}) & 0x3F);
  }
  else if ($h <= 0xEF && $index < $len - 2) {
    $bytes = 3;
    return ($h & 0x0F) << 12 | (ord($c{$index +
1}) & 0x3F) << 6
                             | (ord($c{$index + 2}) &
0x3F);
  }           
  else if ($h <= 0xF4 && $index < $len - 3) {
    $bytes = 4;
    return ($h & 0x0F) << 18 | (ord($c{$index +
1}) & 0x3F) << 12
                             | (ord($c{$index + 2}) &
0x3F) << 6
                             | (ord($c{$index + 3}) &
0x3F);
  }
  else
    return false;
}

?>
----
Server IP: 69.147.83.197
Probable Submitter: 71.168.66.105
----
Manual Page -- http://
www.php.net/manual/en/function.ord.php
Edit        -- https://master
.php.net/note/edit/78032
Del: integrated  -- h
ttps://master.php.net/note/delete/78032/integrated
Del: useless     -- http
s://master.php.net/note/delete/78032/useless
Del: bad code    -- htt
ps://master.php.net/note/delete/78032/bad+code
Del: spam        -- https:/
/master.php.net/note/delete/78032/spam
Del: non-english -- 
https://master.php.net/note/delete/78032/non-english
Del: in docs     -- http
s://master.php.net/note/delete/78032/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78032
Reject      -- https://mast
er.php.net/note/reject/78032
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )