PHP之string之chr()函数使用
chr
- (PHP 4, PHP 5, PHP 7)
- chr — Return a specific character
- chr — 返回指定的字符
Description
string chr ( int $ascii )
//Returns a one-character string containing the character specified by ascii.
//返回相对应于 ascii 所指定的单个字符。
//This function complements ord().
//此函数与 ord() 是互补的。
Parameters
ascii
The extended ASCII code.
Ascii 码。
Values outside the valid range (0..255) will be bitwise and'ed with 255, which is equivalent to the following algorithm:
while ($ascii < 0) {
$ascii += 256;
}
$ascii %= 256;
Return Values
- Returns the specified character.
- 返回规定的字符。
Examples
<?php
/**
* Created by PhpStorm.
* User: zhangrongxiang
* Date: 2018/2/15
* Time: 下午6:56
*/
for ( $i = 65; $i < 127; $i ++ ) {
echo chr( $i ) . ' ';
if ( $i % 10 == 0 ) {
echo PHP_EOL;
}
}
/*
* A B C D E F
* G H I J K L M N O P
* Q R S T U V W X Y Z
* [ \ ] ^ _ ` a b c d
* e f g h i j k l m n
* o p q r s t u v w x
* y z { | } ~
*/
echo PHP_EOL;
function unichr( $dec ) {
if ( $dec < 128 ) {
$utf = chr( $dec );
} else if ( $dec < 2048 ) {
$utf = chr( 192 + ( ( $dec - ( $dec % 64 ) ) / 64 ) );
$utf .= chr( 128 + ( $dec % 64 ) );
} else {
$utf = chr( 224 + ( ( $dec - ( $dec % 4096 ) ) / 4096 ) );
$utf .= chr( 128 + ( ( ( $dec % 4096 ) - ( $dec % 64 ) ) / 64 ) );
$utf .= chr( 128 + ( $dec % 64 ) );
}
return $utf;
}
//中
echo unichr( 20013 ) . PHP_EOL;
//A
echo chr( 321 ) . PHP_EOL;//A 256 + 65 = 321
function genPass( $len = 8 ) {
$passwd = '';
for ( $i = 0; $i <= $len; $i ++ ) {
$passwd = sprintf( '%s%c', isset( $passwd ) ? $passwd : null, rand( 48, 122 ) );
}
return $passwd;
}
//vuTR<oUn;
echo genPass( 8 ) . PHP_EOL;
function unichr2( $dec ) {
if ( $dec < 0x80 ) {
$utf = chr( $dec );
} else if ( $dec < 0x0800 ) {
$utf = chr( 0xC0 + ( $dec >> 6 ) );
$utf .= chr( 0x80 + ( $dec & 0x3f ) );
} else if ( $dec < 0x010000 ) {
$utf = chr( 0xE0 + ( $dec >> 12 ) );
$utf .= chr( 0x80 + ( ( $dec >> 6 ) & 0x3f ) );
$utf .= chr( 0x80 + ( $dec & 0x3f ) );
} else if ( $dec < 0x200000 ) {
$utf = chr( 0xF0 + ( $dec >> 18 ) );
$utf .= chr( 0x80 + ( ( $dec >> 12 ) & 0x3f ) );
$utf .= chr( 0x80 + ( ( $dec >> 6 ) & 0x3f ) );
$utf .= chr( 0x80 + ( $dec & 0x3f ) );
} else {
die( "UTF-8 character size is more than 4 bytes" );
}
return $utf;
}
function unichr3( $u ) {
return mb_convert_encoding( '&#' . intval( $u ) . ';', 'UTF-8', 'HTML-ENTITIES' );
}
echo unichr( 0x263A ) . PHP_EOL;//☺
echo unichr2( 0x263A ) . PHP_EOL;//☺
echo unichr3( 0x263A ) . PHP_EOL;//☺
echo unichr( 0x263B ) . PHP_EOL;//☻
echo unichr2( 0x263B ) . PHP_EOL;//☻
echo unichr( 20013 ) . PHP_EOL;//中
echo unichr2( 20013 ) . PHP_EOL;//中
echo unichr3( 20013 ) . PHP_EOL;//中
See
All rights reserved
PHP之string之chr()函数使用的更多相关文章
- php ord和chr函数
直接上代码 //通过ord()函数获取字符的ASCII码值,如果返回值大于 127则表示为中文字符的一半,再获取后一半组合成一个完整字符 $string = "hello不要迷恋哥world ...
- [Oracle]Oracle之Chr函数返回
Chr函数 返回:返回 String,其中包含有与指定的字符代码相关的字符. chr('39')是单引号 Chr("0") 为0的字符 Chr("1") Chr ...
- python中的ord,chr函数
chr().unichr()和ord() chr()函数用一个范围在range(256)内的(就是0-255)整数作参数,返回一个对应的字符.unichr()跟它一样,只不过返回的是Unicode字符 ...
- Oracle 学习之:ASCII,CHR函数的作用和用法
对于ASCII以及CHR函数的用法,Oracle给出的解释是: ASCII(x)gets the ASCII value of the character X, CHR() and ASCII() h ...
- Asc函数与Chr函数
返回值: Integer 返回字符串中第一个字符的字符代码. 提示: Chr函数可以将一个Ascii码转换为相对应的字符 语法: Asc(string) string,必须参数,字符串 ...
- python中chr()函数和ord()函数的用法
一,chr()函数 格式:Chr(<数值表达式>) 说明:函数返回值类型为String,其数值表达式值取值范围为0~255. 例如:Print Chr(78),结果显示:N. ...
- OC与c混编实现Java的String的hashcode()函数
首先,我不愿意大家需要用到这篇文章里的代码,因为基本上你就是被坑了. 起因:我被Java后台人员坑了一把,他们要对请求的参数增加一个额外的字段,字段的用途是来校验其余的参数是否再传递过程中被篡改或因为 ...
- Oracle中的CHR()函数与ASCII()函数
工作中经常会处理一些因特殊字符而导致的错误,如上周我就遇到了因为换行符和回车符导致的数据上报的错误,这种错误比较难以发现,通常是由于用户的输入习惯导致的,有可能数据极少,就那么几行错误从而导致整个数据 ...
- string类find函数返回值判定
string类find函数返回值判定 代码示例 #include<iostream> #include<cstring> using namespace std; int m ...
随机推荐
- After Upgrade To Release 12.1.3 Users Receive "Function Not Available To This Responsibility" Error While Selecting Sub Menus Under Diagnostics (Doc ID 1200743.1)
APPLIES TO: Oracle Application Object Library - Version 12.1.3 to 12.1.3 [Release 12.1] Information ...
- rzdatetimepicker
两项选为True时可显示今天和圆圈 此属性可使右边按钮下平滑 注意Format中的表示月份的MM一定要大写,不然修改时和分钟会联动.
- write/read/send/receive函数比较
建立好TCP连接后,就可以把得到的套接字当做文件描述符来使用,由此,联系到网络程序里的基本读写函数,write.read: l write函数: Ssize_t write(int fd,const ...
- task4:结对项目-词频统计
结对人:周楠 思路:利用TreeMap实现key字典序,然后输出到LinkedList,然后用Comparator,实现字典值从大到小排序,但是key实现值相同的key字典序的想出的实现方法,但是一直 ...
- Sqlite 快速插入数据到本地表中
用原始Insert方法太慢,网上找到了https://www.cnblogs.com/yisen-code/p/6897524.html 思路是: 开启事务,开启预处理,然后把SQL用参数传入具体值来 ...
- js练习计算器
js练习计算器,支持鼠标点击.键盘操作 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...
- 配置IIS Express,支持JSON
方法有2种: 1. 命令行 a. cd "iis express的安装目录" 例如:cd C:\Program Files (x86)\IIS Express b. appcmd ...
- docker查看挂载目录命令
docker inspect -f "{{.Mounts}}" 692691b7416 692691b7416为containerId
- C#中Cookies的读取
C#中Cookies的读取 链接: 一 .写入Cookie 1. Name 和 Value 属性由程序设定,默认值都是空引用. 2. Domain属性的默认值为当前URL的域名部分,不管发出这个c ...
- JSOI2008 Blue Mary开公司 | 李超线段树学习笔记
题目链接:戳我 这相当于是一个李超线段树的模板qwqwq,题解就不多说了. 代码如下: #include<iostream> #include<cstdio> #include ...