分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

PHP之string之chr()函数使用

发布时间:2023-09-06 01:44责任编辑:胡小海关键词:PHP

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;//Aecho chr( 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 ); ???} 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

  • http://php.net/manual/en/function.chr.php

All rights reserved

PHP之string之chr()函数使用

原文地址:https://www.cnblogs.com/zhangrxiang/p/8495269.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved