search for in the  
<number_formatparse_str>
Last updated: Thu, 19 May 2005

ord

(PHP 3, PHP 4, PHP 5)

ord -- Return ASCII value of character

Description

int ord ( string string )

Returns the ASCII value of the first character of string. This function complements chr().

Example 1. ord() example

<?php
$str
= "\n";
if (
ord($str) == 10) {
   echo
"The first character of \$str is a line feed.\n";
}
?>

You can find an ASCII-table over here: http://www.asciitable.com.

See also chr().



User Contributed Notes
ord
Siegert Naber
22-Mar-2005 07:08
The email-encoding function, a little extended:

# $strEmail. The E-mail address to encode.
# $strDisplay. What will be displayed in the browser. If omitted it takes the e-mail address as it's value.
# $blnCreateLink. Set to true to creates a link. Set to false (and omit $strDisplay) just displays the e-mail address.

function asciiEncodeEmail($strEmail,$strDisplay,$blnCreateLink) {
   $strMailto = "&#109;&#097;&#105;&#108;&#116;&#111;&#058;";
   for ($i=0; $i < strlen($strEmail); $i++) {
       $strEncodedEmail .= "&#".ord(substr($strEmail,$i)).";";
   }
   if(strlen(trim($strDisplay))>0) {
       $strDisplay = $strDisplay;
   }
   else {
       $strDisplay = $strEncodedEmail;
   }
   if($blnCreateLink) {
       return "<a href=\"".$strMailto.$strEncodedEmail."\">".$strDisplay."</a>";
   }
   else {
       return $strDisplay;
   }
}

#examples:
echo asciiEncodeEmail("yourname@yourdomain.com","Your Name",true);
echo "<br />"
echo asciiEncodeEmail("yourname@yourdomain.com","",true);
echo "<br />"
echo asciiEncodeEmail("yourname@yourdomain.com","",false);
skissane at gmail dot com
07-Mar-2005 09:16
Contrary to what jacobfri says below, ord does not use any particular character encoding. It simply converts bytes from a string type to the corresponding integer. These bytes could be characters in ASCII, or some other 7/8-bit code (e.g. ISO-8859-1), or bytes making up characters in some multibyte code (e.g. UTF-8). It all depends on what character encoding your application is using.
28-Feb-2005 08:12
Function using ord() to strip out garbage characters and punctuation from a string. This is handy when trying to be smart about what an input is "trying" to be..
<?

function cleanstr($string){
  
$len = strlen($string);
   for(
$a=0; $a<$len; $a++){
      
$p = ord($string[$a]);
      
# chr(32) is space, it is preserved..
      
(($p > 64 && $p < 123) || $p == 32) ? $ret .= $string[$a] : $ret .= "";
   }
   return
$ret;
}

?>
ng4rrjanbiah at rediffmail dot com
05-Oct-2004 03:01
[Fixed a bug in my previous note; ord() is missing in first condition]

uniord() function like "v0rbiz at yahoo dot com" (Note# 42778), but without using mbstring extension. Note: If the passed character is not valid, it may throw "Uninitialized string offset" notice (may set the error reporting to 0).

<?php
/**
 * @Algorithm: http://www1.tip.nl/~t876506/utf8tbl.html
 * @Logic: UTF-8 to Unicode conversion
 **/
function uniord($c)
{
 
$ud = 0;
  if (
ord($c{0})>=0 && ord($c{0})<=127)
  
$ud = ord($c{0});
  if (
ord($c{0})>=192 && ord($c{0})<=223)
  
$ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
  if (
ord($c{0})>=224 && ord($c{0})<=239)
  
$ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
  if (
ord($c{0})>=240 && ord($c{0})<=247)
  
$ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
  if (
ord($c{0})>=248 && ord($c{0})<=251)
  
$ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
  if (
ord($c{0})>=252 && ord($c{0})<=253)
  
$ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
  if (
ord($c{0})>=254 && ord($c{0})<=255) //error
  
$ud = false;
 return
$ud;
}

//debug
echo uniord('A'); //65
echo uniord("\xe0\xae\xb4"); //2996
?>

HTH,
R. Rajesh Jeba Anbiah
ng4rrjanbiah at rediffmail dot com
01-Jul-2004 06:26
uniord() function like "v0rbiz at yahoo dot com" (Note# 42778), but without using mbstring

<?php
/**
 * @Algorithm http://www1.tip.nl/~t876506/utf8tbl.html
 * @Logic UTF-8 to Unicode conversion
 **/
function uniord($c)
{
 
$ud = 0;
  if (
ord($c{0})>=0 && ord($c{0})<=127)
  
$ud = $c{0};
  if (
ord($c{0})>=192 && ord($c{0})<=223)
  
$ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
  if (
ord($c{0})>=224 && ord($c{0})<=239)
  
$ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
  if (
ord($c{0})>=240 && ord($c{0})<=247)
  
$ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
  if (
ord($c{0})>=248 && ord($c{0})<=251)
  
$ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
  if (
ord($c{0})>=252 && ord($c{0})<=253)
  
$ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
  if (
ord($c{0})>=254 && ord($c{0})<=255) //error
  
$ud = false;
 return
$ud;
}
?>

HTH,
R. Rajesh Jeba Anbiah
bisqwit at iki dot fi
18-Jun-2004 10:23
In reply to jacobfri, the range 128-255 equals to whatever character set you are using.
Try toying with header('Content-type: text/html; charset=iso-8859-1'); and replacing that iso-8859-1 with values like cp850, and suddenly your bytes might start looking surprisingly similar to the ones at www.asciitable.com.
Strings don't store glyphs. They only store bytes. It's the matter of your UA/terminal of deciding what alphabets those bytes are made to look like.
That's where the character set/encoding steps in.
jacobfri at skydebanen dot net
04-Jun-2004 07:10
Just to get things straight about which character table ord() and chr() uses.
The range 128-255 is _not_ equivalent with the widely used extended ASCII-table, like the one described in www.asciitable.com. The actual equivalent is the 128-255 range of Unicode.
That's a good thing, because then ord() and chr() is compatible with javascript, and any other language that uses Unicode.
But it's rather nice to know it, and the description of ord() is kind of misleading, when it only refers to www.asciitable.com.
v0rbiz at yahoo dot com
28-May-2004 06:15
I did not found a unicode/multibyte capable 'ord' function, so...

function uniord($u) {
   $k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8');
   $k1 = ord(substr($k, 0, 1));
   $k2 = ord(substr($k, 1, 1));
   return $k2 * 256 + $k1;
}
merwetta_at_hotmail.com
17-Aug-2002 11:01
In the notes for bin2hex, there is a function for hex encoding an email address in a "mailto" tag to avoid spam bots. The hex encoding works in the anchor tag itself, but not for the link text. To display the email address as the link text, you can use the function below to ascii encode the address and keep the spam bots at bay.

function ascii_encode($string)  {
   for ($i=0; $i < strlen($string); $i++)  {
       $encoded .= '&#'.ord(substr($string,$i)).';';   
   }
   return $encoded;
}
adam at adeptsoftware dot com
16-Jun-2002 11:05
The above comment about bindec is wrong, I think.  bindec accepts a string containing a binary number, not a "binary string" - right?
We need to clean up this terminology.

If you just want to extract a dword/long int from a binary string, this is more accurate (intel endian):

$Number = ord($Buffer{0}) | (ord($Buffer{1})<<8) | (ord($Buffer{2})<<16) | (ord($Buffer{3})<<24);
carl at nospam dot thep dot lu dot se
16-Aug-2001 02:06
If you find that you use this function a lot you really out to consider doing whatever you're doing in C instead. :-)
At least it takes more effort to type "ord($var)-ord('a')" than "var - 'a'".
But hey, you can't get everything you wish for, and at least PHP script very seldom manage to segfault. :-D
theanimus at btconnect dot com
07-Jul-2001 01:01
[Ed: bindec() does this for you... it only doesn't get the sign-bit. Your solution will result in a float with the sign is set!
http://www.php.net/bindec
--jeroen ]

Erm this one took me a while to work out, in the end a friend told me, if ur working out the value of an 32bit integer ($data) then this is 4 u ;-)

$num=ord($data[0])+(ord($data[1])<<8)+(ord($data[2])<<16)+(ord($data[3])<<24);
if ($num>=4294967294){
$num-=4294967296;
}

<number_formatparse_str>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT