码迷,mamicode.com
首页 > Web开发 > 详细

PHP之string之chr()函数使用

时间:2018-03-02 23:17:27      阅读:430      评论:0      收藏:0      [点我收藏+]

标签:sprintf   storm   null   htm   string   nic   specific   parameter   example   

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()函数使用

标签:sprintf   storm   null   htm   string   nic   specific   parameter   example   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!