php ord函数,PHP之string之ord()函数使用

php ord函数,PHP之string之ord()函数使用ord(PHP4,PHP5,PHP7)ord—ReturnASCIIvalueofcharacterord—返回字符的ASCII码值Descriptionintord(string$string)//ReturnstheASCIIvalueofthefirstcharacterofstring.//返回字符串string第一个字符的A…

大家好,欢迎来到IT知识分享网。php ord函数,PHP之string之ord()函数使用

ord

(PHP 4, PHP 5, PHP 7)

ord — Return ASCII value of character

ord — 返回字符的 ASCII 码值

Description

int ord ( string $string )

//Returns the ASCII value of the first character of string.

//返回字符串 string 第一个字符的 ASCII 码值。

//This function complements chr().

//该函数是 chr() 的互补函数。

Parameters

string

A character.

一个字符。

Return Values

Returns the ASCII value as an integer.

返回整型的 ASCII 码值。

Examples

/**

* Created by PhpStorm.

* User: zhangrongxiang

* Date: 2018/2/18

* Time: 下午9:43

*/

$str = ‘abcdef’;

for ( $i = 0; $i < strlen( $str ); $i ++ ) {

echo $str[ $i ] . ‘ : ‘ . ord( $str{$i} ) . PHP_EOL;

}

/*

* a : 97

* b : 98

* c : 99

* d : 100

* e : 101

* f : 102

*/

echo ord( ‘a’ ) . PHP_EOL;//97

echo ord( ‘b’ ) . PHP_EOL;//98

function ordutf8( $string, &$offset ) {

$code = ord( substr( $string, $offset, 1 ) );

$bytesnumber = 0;

if ( $code >= 128 ) { //otherwise 0xxxxxxx

if ( $code < 224 ) {

$bytesnumber = 2;

} //110xxxxx

else if ( $code < 240 ) {

$bytesnumber = 3;

} //1110xxxx

else if ( $code < 248 ) {

$bytesnumber = 4;

} //11110xxx

$codetemp = $code – 192 – ( $bytesnumber > 2 ? 32 : 0 ) – ( $bytesnumber > 3 ? 16 : 0 );

for ( $i = 2; $i <= $bytesnumber; $i ++ ) {

$offset ++;

$code2 = ord( substr( $string, $offset, 1 ) ) – 128; //10xxxxxx

$codetemp = $codetemp * 64 + $code2;

}

$code = $codetemp;

}

$offset += 1;

if ( $offset >= strlen( $string ) ) {

$offset = – 1;

}

return $code;

}

$text = “中国?”;

$offset = 0;

while ( $offset >= 0 ) {

//0: 20013

//3: 22269

//6: 128516

echo $offset . “: ” . ordutf8( $text, $offset ) . PHP_EOL;

}

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;

}

echo uniord( “中” ) . PHP_EOL;//20013

echo uniord( “国” ) . PHP_EOL;//22269

//error

echo uniord( ‘?’ ) . PHP_EOL;//63

See

All rights reserved

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/16100.html

(0)
上一篇 2024-02-10 22:00
下一篇 2024-02-13 13:33

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信