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 ...
随机推荐
- [leetcode] 8. Maximum Depth of Binary Tree
可能是因为我是按难度顺序刷的原因,这个其实在之前的几道题里面已经写过了.题目如下: Given a binary tree, find its maximum depth. The maximum d ...
- scvmm sdk之ddtkh(二)
ddtkh,dynamic datacenter toolkit for hosters,原先发布在codeplex开源社区,后来被微软归档到开发者社区中,从本质上来说它是一个企业级应用的套件,集成了 ...
- Solr相似度算法二:Okapi BM25
地址:https://en.wikipedia.org/wiki/Okapi_BM25 In information retrieval, Okapi BM25 (BM stands for Be ...
- asp.net——Josn转DataTable(转)
使用UI框架开发的时候就常常用到DataTable转Json的情况,但是最近完成一个微信公众号开发的项目,需要把微信接口传过来的json值作为转为DataTable后绑定到服务器控件上. 在网上找了很 ...
- sql-修改每条数据的某一个字段的值
update B set B.maildata =(select SUBSTRING(maildata,0,3) from basedata where basedata.cid = B.cid)+( ...
- 解决WebService中System.InvalidOperationException:缺少参数的问题
此问题在.Net 4.0 IIS7 Windows Server 2008下可能会出现. 现象是第一次正常调用,第二次接口报错. 删除CacheDuration即可.
- PHP 代码优化测试【Benchmark数据测试】
由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 : --> 点击这里 Benchmark测试之前我们先来了解Benchmark.直接下载:ht ...
- Java容器中的元素输出
1.容器不同于数组,容器若是想输出全部元素,可以直接利用System.out.println(collection) public class TestCollectionArrayPrint { p ...
- JAVA日期——java.util.date类的操作
package com.hxzy.time; import java.text.SimpleDateFormat;import java.util.Date; public class DateDem ...
- WebLogic “Java 反序列化”过程远程命令执行
WebLogic “Java 反序列化”过程远程命令执行 详细信息: https://www.seebug.org/vuldb/ssvid-89726 说明: 反序列化是指特定语言中将传递的对象序列化 ...