note 72463 added to function.ord

From: Date: Fri, 19 Jan 2007 19:27:03 +0000
Subject: note 72463 added to function.ord
Groups: php.notes 
Request: Send a blank email to php-notes+get-121925@lists.php.net to get a copy of this message
I found I wanted to sanitize a string for certain ASCII/ANSI characters, but to leave unicode alone. Since ord() breaks on processing unicode, I drew these two functions up to help with a saniziter which looked at ordinal values. (Finding "pack" and "unpack" was much better than my own powers-of-256 code.) <?php /* By Darien Hager, Jan 2007... Use however you wish, but please please give credit in source comments. Change "UTF-8" to whichever encoding you are expecting to use. */ function ords_to_unistr($ords, $encoding = 'UTF-8'){ // Turns an array of ordinal values into a string of unicode characters $str = ''; for($i = 0; $i < sizeof($ords); $i++){ // Pack this number into a 4-byte string // (Or multiple one-byte strings, depending on context.) $v = $ords[$i]; $str .= pack("N",$v); } $str = mb_convert_encoding($str,$encoding,"UCS-4BE"); return($str); } function unistr_to_ords($str, $encoding = 'UTF-8'){ // Turns a string of unicode characters into an array of ordinal values, // Even if some of those characters are multibyte. $str = mb_convert_encoding($str,"UCS-4BE",$encoding); $ords = array(); // Visit each unicode character for($i = 0; $i < mb_strlen($str,"UCS-4BE"); $i++){ // Now we have 4 bytes. Find their total // numeric value. $s2 = mb_substr($str,$i,1,"UCS-4BE"); $val = unpack("N",$s2); $ords[] = $val[1]; } return($ords); } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 216.251.108.82 ---- Manual Page -- http://www.php.net/manual/en/function.ord.php Edit -- https://master.php.net/note/edit/72463 Del: integrated -- https://master.php.net/note/delete/72463/integrated Del: useless -- https://master.php.net/note/delete/72463/useless Del: bad code -- https://master.php.net/note/delete/72463/bad+code Del: spam -- https://master.php.net/note/delete/72463/spam Del: non-english -- https://master.php.net/note/delete/72463/non-english Del: in docs -- https://master.php.net/note/delete/72463/in+docs Del: other reasons-- https://master.php.net/note/delete/72463 Reject -- https://master.php.net/note/reject/72463 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#121925) next »