Php character encode string

Php character encode string

chr — Generate a single-byte string from a number

Description

Returns a one-character string containing the character specified by interpreting codepoint as an unsigned integer.

This can be used to create a one-character string in a single-byte encoding such as ASCII, ISO-8859, or Windows 1252, by passing the position of a desired character in the encoding’s mapping table. However, note that this function is not aware of any string encoding, and in particular cannot be passed a Unicode code point value to generate a string in a multibyte encoding like UTF-8 or UTF-16.

This function complements ord() .

Parameters

An integer between 0 and 255.

Values outside the valid range (0..255) will be bitwise and’ed with 255, which is equivalent to the following algorithm:

Return Values

A single-character string containing the specified byte.

Changelog

Version Description
7.4.0 The function no longer silently accepts unsupported codepoint s, and casts these to 0 .

Examples

Example #1 chr() example

// Assumes the string will be used as ASCII or an ASCII-compatible encoding

$str = «The string ends in escape: » ;
$str .= chr ( 27 ); /* add an escape character at the end of $str */

/* Often this is more useful */

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

Example #2 Overflow behavior

The above example will output:

Example #3 Building a UTF-8 string from individual bytes

The above example will output:

See Also

  • sprintf() — Return a formatted string with a format string of %c
  • ord()
  • An » ASCII-table
  • mb_chr()
  • IntlChar::chr()

User Contributed Notes 23 notes

Note that if the number is higher than 256, it will return the number mod 256.
For example :
chr(321)=A because A=65(256)

for ( $i = 128 ; $i $str = chr ( 240 ) . chr ( 159 ) . chr ( 144 ) . chr ( $i );
echo $str ;
>

Another quick and short function to get unicode char by its code.

/**
* Return unicode char by its code
*
* @param int $u
* @return char
*/
function unichr ( $u ) return mb_convert_encoding ( ‘&#’ . intval ( $u ) . ‘;’ , ‘UTF-8’ , ‘HTML-ENTITIES’ );
>
?>

I spent hours looking for a function which would take a numeric HTML entity value and output the appropriate UTF-8 bytes. I found this at another site and only had to modify it slightly; so I don’t take credit for this.

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 ;
> ?>

So for example:

Want terminal colors in command line php scripts?

This should take care of that.

$_colors = array(
‘LIGHT_RED’ => «[1;31m»,
‘LIGHT_GREEN’ => «[1;32m»,
‘YELLOW’ => «[1;33m»,
‘LIGHT_BLUE’ => «[1;34m»,
‘MAGENTA’ => «[1;35m»,
‘LIGHT_CYAN’ => «[1;36m»,
‘WHITE’ => «[1;37m»,
‘NORMAL’ => «[0m»,
‘BLACK’ => «[0;30m»,
‘RED’ => «[0;31m»,
‘GREEN’ => «[0;32m»,
‘BROWN’ => «[0;33m»,
‘BLUE’ => «[0;34m»,
‘CYAN’ => «[0;36m»,
‘BOLD’ => «[1m»,
‘UNDERSCORE’ => «[4m»,
‘REVERSE’ => «[7m»,

Читайте также:  Multipart form data java rest

function termcolored($text, $color=»NORMAL», $back=1) <
global $_colors;
$out = $_colors[«$color»];
if($out == «») < $out = "[0m"; >
if($back) <
return chr(27).»$out$text».chr(27).chr(27).»[0m».chr(27);
>else <
echo chr(27).»$out$text».chr(27).chr(27).»[0m».chr(27);
>//fi
>// end function

echo termcolored(«test\n», «BLUE»);
?>

Here is a sample of encoding and decoding using «chr» and «ord».
function Encode ( $txtData , $Level ) for ( $j = 0 ; $j < $Level ; $j ++)$tmpStr = '' ;
for ( $i = 0 ; $i < strlen ( $txtData ); $i ++)
$tmpStr .= ord ( substr ( strtoupper ( $txtData ), $i , 1 ));
$txtData = $tmpStr ;
>
return ( strlen ( $Level )). $Level . $txtData ;
>

function Decode ( $txtData ) $intLevel = substr ( $txtData , 1 , substr ( $txtData , 0 , 1 ));
$startStr = substr ( $txtData , substr ( $txtData , 0 , 1 )+ 1 , strlen ( $txtData ));
for ( $j = 0 ; $j < $intLevel ; $j ++)for ( $i = 0 ; $i < strlen ( $startStr ); $i += 2 )
$tmpStr .= chr ( intval ( substr ( $startStr , $i , 2 )));
$startStr = $tmpStr ;

$tmpStr = «» ;
>
return $startStr ;
>

echo Encode ( ‘123’ , 4 ). ‘
‘ ;
echo Decode ( Encode ( ‘123’ , 5 ));
?>

Secure password generator with a variable maximum amount of symbols.

function passwdGen ( $minLength = 8 , $maxLength = 12 , $maxSymbols = 2 )
$symbolCount = 0 ;

srand ((double) microtime () * 1000003 );

$passwd = sprintf ( ‘%s%c’ , isset( $passwd ) ? $passwd : NULL , $char );
>

In addition to replacing Microsoft Windows smart quotes, as sgaston demonstrated on 2006-02-13, I replace all other Microsoft Windows characters using suggestions[1] published by character code specialist[2] Jukka Korpela.

$str = str_replace ( chr ( 130 ), ‘,’ , $str ); // baseline single quote
$str = str_replace ( chr ( 131 ), ‘NLG’ , $str ); // florin
$str = str_replace ( chr ( 132 ), ‘»‘ , $str ); // baseline double quote
$str = str_replace ( chr ( 133 ), ‘. ‘ , $str ); // ellipsis
$str = str_replace ( chr ( 134 ), ‘**’ , $str ); // dagger (a second footnote)
$str = str_replace ( chr ( 135 ), ‘***’ , $str ); // double dagger (a third footnote)
$str = str_replace ( chr ( 136 ), ‘^’ , $str ); // circumflex accent
$str = str_replace ( chr ( 137 ), ‘o/oo’ , $str ); // permile
$str = str_replace ( chr ( 138 ), ‘Sh’ , $str ); // S Hacek
$str = str_replace ( chr ( 139 ), ‘ $str = str_replace ( chr ( 140 ), ‘OE’ , $str ); // OE ligature
$str = str_replace ( chr ( 145 ), «‘» , $str ); // left single quote
$str = str_replace ( chr ( 146 ), «‘» , $str ); // right single quote
$str = str_replace ( chr ( 147 ), ‘»‘ , $str ); // left double quote
$str = str_replace ( chr ( 148 ), ‘»‘ , $str ); // right double quote
$str = str_replace ( chr ( 149 ), ‘-‘ , $str ); // bullet
$str = str_replace ( chr ( 150 ), ‘-‘ , $str ); // endash
$str = str_replace ( chr ( 151 ), ‘—‘ , $str ); // emdash
$str = str_replace ( chr ( 152 ), ‘~’ , $str ); // tilde accent
$str = str_replace ( chr ( 153 ), ‘(TM)’ , $str ); // trademark ligature
$str = str_replace ( chr ( 154 ), ‘sh’ , $str ); // s Hacek
$str = str_replace ( chr ( 155 ), ‘>’ , $str ); // right single guillemet
$str = str_replace ( chr ( 156 ), ‘oe’ , $str ); // oe ligature
$str = str_replace ( chr ( 159 ), ‘Y’ , $str ); // Y Dieresis
?>

Читайте также:  Python close all threads
[1] On the use of some MS Windows characters in HTML
http://www.cs.tut.fi/~jkorpela/www/windows-chars.html

Here is a function that’s help me find what chr(number) outputs what character quicker than typing out 256 echo tags.

function listChr () <
for ( $i = 0 ; $i < 256 ; ++ $i ) <
static $genNum ;
$genNum ++;
echo «chr( $genNum ) will output ‘» ;
echo ( chr ( $genNum ));
echo «‘< br>\n» ;
>
>
listChr ();
?>

Another helpful chr is #9, being a tab. Quite using when making error logs.

$tab = (chr(9));
echo «

errordatetime

«;

// rivencodec 1.0
// encode riverse ascii 1 simple function can encode/decode
// can use it for secure source with speed encode text

function rivencodec ( $ch , $a = 0 ) while((@ $b = $ch [ $a ++])) < $ch [ $a - 1 ] = chr ( 255 - ord ( $b )); >
return $ch ;
>

$zz = rivencodec ( «abcdefghijklmn» );
echo ‘encode: ‘ , $zz , ‘
‘ , PHP_EOL ;

$yy = rivencodec ( $zz );
echo ‘decode: ‘ , $yy , ‘
‘ , PHP_EOL ;

to remove the ASCII control characters (except «line feed» and «tab») :

Note that chr(10) is a ‘line feed’ and chr(13) is a ‘carriage return’ and they are not the same thing! I found this out while attempting to parse text from forms and text files for inclusion as HTML by replacing all the carriage returns with
‘s only to find after many head-scratchings that I should have been looking for line feeds. If anyone can shed some light on what the difference is, please do.


If you’re planning on saving text from a form into a database for later display, you’ll need to apply the following function so that it gets saved with the proper HTML tags.

$text = str_replace ( chr ( 10 ), «» , $text );
?>

When you want to plug it back into that form for editing you need to convert it back.

$text = str_replace ( «» , chr ( 10 ), $text )
?>

Hope this saves somebody some trouble. 🙂

Simple password generation function using sprintf and the %c type specifier; which is the same as chr().

function genPass($len = 8) for ($i=0;$i <=$len;$i++) $passwd = sprintf('%s%c', isset($passwd) ? $passwd : NULL, rand(48, 122));
>
return $passwd;
>

argument is automatically converted to integer, so chr(’65’) and chr(65) would both output the letter A

When having to deal with parsing an IIS4 or IIS5 metabase dump I wrote a simple function for converting those MS hexidecimal values into their ascii counter parts. Hopefully someone will find use for it.

function hex_decode ( $string ) <
for ( $i = 0 ; $i < strlen ( $string ); $i ) <
$decoded .= chr ( hexdec ( substr ( $string , $i , 2 )));
$i = (float)( $i )+ 2 ;
>
return $decoded ;
>
?>

%c is defined as: «Print the character belonging to the ascii code given»

chr() just gives a string, so you need to use %s, even if the string consists of only one character. This is consistent with other languages.
—Jeroen@php.net]

Learn from my mistake:
Do not expect this to work!

$c_question = chr ( 63 );
$v_out = sprintf ( » //. more stuff being sprintf’d into v_out here .
$v_out = sprintf ( «%s%c>\n» , $v_out , $c_question );
$v_fp = fopen ( «foofile» , «w» );
if ( $v_fp )
<
fwrite ( $v_fp , $v_out , strlen ( $v_out ));
fclose ( $v_fp );
>
?>

Читайте также:  Text javascript or text jscript

When I did this, foofile contained .
I spun my wheels quite awhile looking at fputs, fwrite to verify I was calling those functions correctly.
My mistake was using $c_question = chr(63) instead of
$c_question = 63 (correct). Then everything worked fine.

string mb_chr ( int $cp [, string $encoding ] )
Parameter List:
cp — character code (in decimal notation)
encoding — encoding (UTF-8, ASCII and so on)

We get the letter ‘Ж’ from the encoding UTF-8:

$sim = mb_chr(0x0416, ‘UTF-8’);
echo $sim; // Ж

Get the character ‘>’ from the encoding ASCII:

$sim = mb_chr(125, ‘ASCII’);
echo $sim ; // >

Источник

PHP UTF-8 Conversion

PHP UTF-8 Conversion

  1. Use utf8_encode() and utf8_decode() to Encode and Decode Strings in PHP
  2. Use iconv() to Convert a String to UTF-8

The UTF-8 is a way to encode Unicode characters, each character in between one to four bytes.

It is used to handle the special character or characters from languages other than English.

PHP has different ways to convert text into UTF-8 .

Use utf8_encode() and utf8_decode() to Encode and Decode Strings in PHP

Both utf8_encode() and utf8_decode() are built-in functions in PHP.

It is used to encode and decode ISO-8859-1 , and other types of strings to UTF-8 , both of these function takes a string as a parameter.

php $demo="\xE0\xE9\xED"; //ISO-8859-1 String àéí  echo "UTF-8 Encoded String: "; echo utf8_encode($demo) ."
"
;
echo "UTF-8 Decoded String: "; echo utf8_decode(utf8_encode($demo)) ."
"
;
echo "UTF-8 Encoded String from the decoded: "; echo utf8_encode(utf8_decode(utf8_encode($demo))) ."
"
;
?>

The code above encodes an ISO-8859-1 string to UTF and then decodes the output again. The input string you see is with ISO-8859-1 encoding.

UTF-8 Encoded String: àéí UTF-8 Decoded String: ��� UTF-8 Encoded String from the decoded: àéí 

The utf8_decode() converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1 .

When reading an ISO-8859-1 encoded text as UTF-8 , you will often see that question mark.

Use iconv() to Convert a String to UTF-8

iconv() is another built-in PHP function used to convert string from one Unicode.

It takes three parameters, one is the string’s Unicode, the second is the Unicode you want to convert, and the third is the string itself.

php $demo="\xE0\xE9\xED"; //ISO-8859-1 String àéí  echo "The UTF-8 String is: "; echo iconv("ISO-8859-1", "UTF-8", $demo)."
"
;
//mb_detect_encoding() is a function used to detect encoding of the given text. echo "The UTF-8 String with auto detection is: "; echo iconv(mb_detect_encoding($demo, mb_detect_order(), true), "UTF-8", $demo); ?>

The code above takes three parameters and converts the text to UTF-8 .

The UTF-8 String is: àéí The UTF-8 String with auto detection is: àéí 

PHP also offers other functions like recode_string() or mb_convert_encoding() , which works similarly to iconv ; they convert a string to the requested Unicode.

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

Related Article — PHP Encode

Источник

Оцените статью