PHP之string之chr()函数使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP之string之chr()函数使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2633字,纯文字阅读大概需要4分钟。
内容图文
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 ++ ) {
echochr($i) . ' ';if($i % 10 == 0) {
echoPHP_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 { | } ~ */echoPHP_EOL;function unichr($dec) {
if($dec < 128) {
$utf = chr($dec);
} elseif($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;//Aechochr(321) . PHP_EOL;//A 256 + 65 = 321function 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);
} elseif($dec < 0x0800) {
$utf = chr(0xC0 + ($dec >> 6));$utf .= chr(0x80 + ($dec & 0x3f));
} elseif($dec < 0x010000) {
$utf = chr(0xE0 + ($dec >> 12));$utf .= chr(0x80 + (($dec >> 6) & 0x3f));$utf .= chr(0x80 + ($dec & 0x3f));
} elseif($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) {
returnmb_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
原文:https://www.cnblogs.com/zhangrxiang/p/8495269.html
内容总结
以上是互联网集市为您收集整理的PHP之string之chr()函数使用全部内容,希望文章能够帮你解决PHP之string之chr()函数使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。