segunda-feira, 20 de fevereiro de 2012

(PHP) Função CHR recriada

Como prometido, eu iria colocar aqui mais funções nativas criadas do zero, conforme o artigo anterior segue o trecho de código para função chr

function chr($i) {
$chrs = array(0 => '\0',1 => ' ',2 => ' ',3 => ' ',4 => ' ',5 => ' ',6 => ' ',7 => ' ',8 => ' ',9 => ' ',10 => ' ',11 => ' ',12 => ' ',13 => ' ',14 => ' ',15 => ' ',16 => ' ',17 => ' ',18 => ' ',19 => ' ',20 => ' ',21 => ' ',22 => ' ',23 => ' ',24 => ' ',25 => ' ',26 => ' ',27 => ' ',28 => ' ',29 => ' ',30 => ' ',31 => ' ',32 => ' ',33 => '!',34 => '\"',35 => '#',36 => '$',37 => '%',38 => '&',39 => '\'',40 => '(',41 => ')',42 => '*',43 => '+',44 => ',',45 => '-',46 => '.',47 => '/',48 => '0',49 => '1',50 => '2',51 => '3',52 => '4',53 => '5',54 => '6',55 => '7',56 => '8',57 => '9',58 => ':',59 => ';',60 => '<',61 => '=',62 => '>',63 => '?',64 => '@',65 => 'A',66 => 'B',67 => 'C',68 => 'D',69 => 'E',70 => 'F',71 => 'G',72 => 'H',73 => 'I',74 => 'J',75 => 'K',76 => 'L',77 => 'M',78 => 'N',79 => 'O',80 => 'P',81 => 'Q',82 => 'R',83 => 'S',84 => 'T',85 => 'U',86 => 'V',87 => 'W',88 => 'X',89 => 'Y',90 => 'Z',91 => '[',92 => '\\',93 => ']',94 => '^',95 => '_',96 => '`',97 => 'a',98 => 'b',99 => 'c',100 => 'd',101 => 'e',102 => 'f',103 => 'g',104 => 'h',105 => 'i',106 => 'j',107 => 'k',108 => 'l',109 => 'm',110 => 'n',111 => 'o',112 => 'p',113 => 'q',114 => 'r',115 => 's',116 => 't',117 => 'u',118 => 'v',119 => 'w',120 => 'x',121 => 'y',122 => 'z',123 => '{',124 => '|',125 => '}',126 => '~',127 => '',128 => '€',129 => '',130 => '‚',131 => 'ƒ',132 => '„',133 => '…',134 => '†',135 => '‡',136 => 'ˆ',137 => '‰',138 => 'Š',139 => '‹',140 => 'Œ',141 => '',142 => 'Ž',143 => '',144 => '',145 => '‘',146 => '’',147 => '“',148 => '”',149 => '•',150 => '–',151 => '—',152 => '˜',153 => '™',154 => 'š',155 => '›',156 => 'œ',157 => '',158 => 'ž',159 => 'Ÿ',160 => ' ',161 => '¡',162 => '¢',163 => '£',164 => '¤',165 => '¥',166 => '¦',167 => '§',168 => '¨',169 => '©',170 => 'ª',171 => '«',172 => '¬',173 => '-',174 => '®',175 => '¯',176 => '°',177 => '±',178 => '²',179 => '³',180 => '´',181 => 'µ',182 => '¶',183 => '·',184 => '¸',185 => '¹',186 => 'º',187 => '»',188 => '¼',189 => '½',190 => '¾',191 => '¿',192 => 'À',193 => 'Á',194 => 'Â',195 => 'Ã',196 => 'Ä',197 => 'Å',198 => 'Æ',199 => 'Ç',200 => 'È',201 => 'É',202 => 'Ê',203 => 'Ë',204 => 'Ì',205 => 'Í',206 => 'Î',207 => 'Ï',208 => 'Ð',209 => 'Ñ',210 => 'Ò',211 => 'Ó',212 => 'Ô',213 => 'Õ',214 => 'Ö',215 => '×',216 => 'Ø',217 => 'Ù',218 => 'Ú',219 => 'Û',220 => 'Ü',221 => 'Ý',222 => 'Þ',223 => 'ß',224 => 'à',225 => 'á',226 => 'â',227 => 'ã',228 => 'ä',229 => 'å',230 => 'æ',231 => 'ç',232 => 'è',233 => 'é',234 => 'ê',235 => 'ë',236 => 'ì',237 => 'í',238 => 'î',239 => 'ï',240 => 'ð',241 => 'ñ',242 => 'ò',243 => 'ó',244 => 'ô',245 => 'õ',246 => 'ö',247 => '÷',248 => 'ø',249 => 'ù',250 => 'ú',251 => 'û',252 => 'ü',253 => 'ý',254 => 'þ',255 => 'ÿ',256 => '\0',257 => ' ',258 => ' ',259 => ' ',260 => ' ',261 => ' ',262 => ' ',263 => ' ',264 => ' ',265 => ' ',266 => ' ',267 => ' ',268 => ' ',269 => ' ',270 => ' ',271 => ' ',272 => ' ',273 => ' ',274 => ' ',275 => ' ',276 => ' ',277 => ' ',278 => ' ',279 => ' ',280 => ' ',281 => ' ',282 => ' ',283 => ' ',284 => ' ',285 => ' ',286 => ' ',287 => ' ',288 => ' ',289 => '!',290 => '\"',291 => '#',292 => '$',293 => '%',294 => '&',295 => '\'',296 => '(',297 => ')',298 => '*',299 => '+',300 => ',',301 => '-',302 => '.',303 => '/',304 => '0',305 => '1',306 => '2',307 => '3',308 => '4',309 => '5',310 => '6',311 => '7',312 => '8',313 => '9',314 => ':',315 => ';',316 => '<',317 => '=',318 => '>',319 => '?',320 => '@',321 => 'A',322 => 'B',323 => 'C',324 => 'D',325 => 'E',326 => 'F',327 => 'G',328 => 'H',329 => 'I',330 => 'J',331 => 'K',332 => 'L',333 => 'M',334 => 'N',335 => 'O',336 => 'P',337 => 'Q',338 => 'R',339 => 'S',340 => 'T',341 => 'U',342 => 'V',343 => 'W',344 => 'X',345 => 'Y',346 => 'Z',347 => '[',348 => '\\',349 => ']',350 => '^',351 => '_',352 => '`',353 => 'a',354 => 'b',355 => 'c',356 => 'd',357 => 'e',358 => 'f',359 => 'g',360 => 'h',361 => 'i',362 => 'j',363 => 'k',364 => 'l',365 => 'm',366 => 'n',367 => 'o',368 => 'p',369 => 'q',370 => 'r',371 => 's',372 => 't',373 => 'u',374 => 'v',375 => 'w',376 => 'x',377 => 'y',378 => 'z',379 => '{',380 => '|',381 => '}',382 => '~',383 => '',384 => '€',385 => '',386 => '‚',387 => 'ƒ',388 => '„',389 => '…',390 => '†',391 => '‡',392 => 'ˆ',393 => '‰',394 => 'Š',395 => '‹',396 => 'Œ',397 => '',398 => 'Ž',399 => '',400 => '',401 => '‘',402 => '’',403 => '“',404 => '”',405 => '•',406 => '–',407 => '—',408 => '˜',409 => '™',410 => 'š',411 => '›',412 => 'œ',413 => '',414 => 'ž',415 => 'Ÿ',416 => ' ',417 => '¡',418 => '¢',419 => '£',420 => '¤',421 => '¥',422 => '¦',423 => '§',424 => '¨',425 => '©',426 => 'ª',427 => '«',428 => '¬',429 => '-',430 => '®',431 => '¯',432 => '°',433 => '±',434 => '²',435 => '³',436 => '´',437 => 'µ',438 => '¶',439 => '·',440 => '¸',441 => '¹',442 => 'º',443 => '»',444 => '¼',445 => '½',446 => '¾',447 => '¿',448 => 'À',449 => 'Á',450 => 'Â',451 => 'Ã',452 => 'Ä',453 => 'Å',454 => 'Æ',455 => 'Ç',456 => 'È',457 => 'É',458 => 'Ê',459 => 'Ë',460 => 'Ì',461 => 'Í',462 => 'Î',463 => 'Ï',464 => 'Ð',465 => 'Ñ',466 => 'Ò',467 => 'Ó',468 => 'Ô',469 => 'Õ',470 => 'Ö',471 => '×',472 => 'Ø',473 => 'Ù',474 => 'Ú',475 => 'Û',476 => 'Ü',477 => 'Ý',478 => 'Þ',479 => 'ß',480 => 'à',481 => 'á',482 => 'â',483 => 'ã',484 => 'ä',485 => 'å',486 => 'æ',487 => 'ç',488 => 'è',489 => 'é',490 => 'ê',491 => 'ë',492 => 'ì',493 => 'í',494 => 'î',495 => 'ï',496 => 'ð',497 => 'ñ',498 => 'ò',499 => 'ó');
return $chrs[$i];
}

A função chr retorna um caractere a partir de um valor decimal, é bastante eficiente para quem deseja gerar strings aleatórias e outros, só depender da criatividade.

A lógica desta função, bem como a outra função ord é apenas uma array que armazena o valor do caractere x valor decimal, depois só pegar o valor da array por um destes índices retornando o que desejamos.

Nenhum comentário:

Postar um comentário