Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

chr

(PHP 3, PHP 4, PHP 5)

chr -- Возвращает символ по его коду

Описание

string chr ( int ascii )

Возвращает строку из одного символа, код которого задан аргументом ascii.

Пример 1. Пример использования chr()

<?php
$str
= "Эта строка окачивается escape: ";
$str .= chr(27); /* добавляет escape-символ в конец $str */

/* Но часто лучше использовать такую конструкцию */

$str = sprintf("The string ends in escape: %c", 27);
?>

Таблицу ASCII-кодов вы можете найти здесь: http://www.asciitable.com.

Эта функция дополняет функцию ord(). См. также описание формата %c функции sprintf().



chunk_split> <chop
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
chr
Miguel Perez
19-Sep-2007 06:48
This is my implementation of unichr:

<?php
function unichr($c) {
    if (
$c <= 0x7F) {
        return
chr($c);
    } else if (
$c <= 0x7FF) {
        return
chr(0xC0 | $c >> 6) . chr(0x80 | $c & 0x3F);
    } else if (
$c <= 0xFFFF) {
        return
chr(0xE0 | $c >> 12) . chr(0x80 | $c >> 6 & 0x3F)
                                    .
chr(0x80 | $c & 0x3F);
    } else if (
$c <= 0x10FFFF) {
        return
chr(0xF0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3F)
                                    .
chr(0x80 | $c >> 6 & 0x3F)
                                    .
chr(0x80 | $c & 0x3F);
    } else {
        return
false;
    }
}
?>
darkodemon at gmail dot com
27-Apr-2007 04:33
chr() with unicode support

<?php

function uchr ($codes) {
    if (
is_scalar($codes)) $codes= func_get_args();
   
$str= '';
    foreach (
$codes as $code) $str.= html_entity_decode('&#'.$code.';',ENT_NOQUOTES,'UTF-8');
    return
$str;
}

echo
uchr(23383); echo '<br/>';
echo
uchr(23383,215,23383); echo '<br/>';
echo
uchr(array(23383,215,23383,215,23383)); echo '<br/>';

?>
vdklah at hotmail dot com
27-Apr-2007 02:02
I'm sorry to say that the posted convertIntToAlphabet function does not work correctly. It fails on numbers with a "Z" in it! Aside that it also gives a PHP warning, I really did not like recursion 'cauz huge numbers leads into unneeded memory usage. Here some results showing problems:

Dec    convertIntToAlphabet
...
24    X
25    Y
26        -> empty!
27    AA
28    AB
...
76    BX
77    BY
78        -> empty!
79    CA
80    CB
...
1350    AYX   
1351    AYY   
1352        -> empty!
1353    A    -> bad!
1354    B    -> bad!

So I have written another version:

    function toAlphaNumber( $num )
    {
        $anum = '';
        while( $num >= 1 ) {
            $num = $num - 1;
            $anum = chr(($num % 26)+65).$anum;
            $num = $num / 26;
        }
        return $anum;
    }

With improved results:

Dec    toAlphaNumber
...
24    X
25    Y
26    Z
27    AA
28    AB
...
76    BX
77    BY
78    BZ
79    CA
80    CB
...
1350    AYX
1351    AYY
1352    AYZ
1353    AZA
1354    AZB
lawright at mac dot com
04-Jan-2007 07:39
A little modification of the really useful onlyreadables function posted by jgraef below that saves some of the common characters..

function onlyreadables($string) {
  for ($i=0;$i<strlen($string);$i++) {
    $chr = $string{$i};
    $ord = ord($chr);

    if ($ord>32 and $ord<126)
        continue;
    elseif ($ord>191 and $ord<198)
        $string{$i} = 'A';
    elseif ($ord>199 and $ord<204)
        $string{$i} = 'E';
    elseif ($ord>203 and $ord<208)
        $string{$i} = 'I';
    elseif ($ord>209 and $ord<215)
        $string{$i} = 'O';
    elseif ($ord>216 and $ord<221)
        $string{$i} = 'U';
    elseif ($ord>223 and $ord<230)
        $string{$i} = 'a';
    elseif ($ord>231 and $ord<236)
        $string{$i} = 'e';
    elseif ($ord>235 and $ord<240)
        $string{$i} = 'i';
    elseif ($ord>241 and $ord<247)
        $string{$i} = 'o';
    elseif ($ord>249 and $ord<253)
        $string{$i} = 'u';
    elseif ($ord>252 and $ord<256)
        $string{$i} = 'y';
    elseif ($ord==241)
        $string{$i} = 'n';
    elseif ($ord==209)
        $string{$i} = 'N';
    else
        $string{$i} = '.';
  }
  return $string;
}
Paddy and Ger
02-Jan-2007 05:35
This is just a small piece that allows you to echo out a set number of characters from a string. We were writing option values (html form element) dynamically from an xml file. Some of the strings were too long and as the select tag has no attributes to control the width or columns it seems a handy way to solve the problem.

$string = "this is some text";

$length = strlen($string);   // gets the length of the string

$newLength = -($length - 5);  // makes the length smaller by 5 characters

$string = substr("$string", 0, $newLength);  // sets the string to be 5 characters in length

echo $string;
23-Nov-2006 02:08
I was really surprised to see that work:

$myurl = preg_replace('~%([0-9a-f])([0-9a-f])~ei', 'chr(hexdec("\\1\\2"))', $myurl);

'M%C3%BCnchen' (which is what a Browser makes of an URL containing verbatim utf-8) gives a nice 2-byte-unicode-char: 'M
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571