note 107470 added to function.preg-match

From: Date: Sat, 11 Feb 2012 03:48:59 +0000
Subject: note 107470 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-186064@lists.php.net to get a copy of this message
<?php /* FIXING THE LENGTH WITH ZERO OR ANY CHARACTERS IN THE GIVEN DATA. This function adds zero's in front of numbers and adds any character at the end of alphanumberic data. ex. 1234 = length 10 ---> output: 0000001234 ex. arman = length 10 ---> "\x65 is letter e" output armaneeeee try to change $withzero by true or false if $withzero = true {it will add zero's before the text} if $withzero = false {it will add any hex character "\x???" in front of text} space = "\x20" or terminate = "\x0"; */ if(isset($_POST['txt'])){ echo scb_FixLength($_POST['txt'], $_POST['txtLength']); } function scb_FixLength($data, $length, $withzero=true) { settype($data,"string"); if(!eregi("[a-zA-Z0-9_-]", $data)){ echo $data . " is not alphanumeric"; return false; } if ($length < strlen($data)) { echo "* input cannot be greater than maximum length of field. "; echo "The maximum length is " . $length . " The data length is " . strlen($data) . " Error in ".$data; return false; } elseif ($withzero==true) { //zero is in front of the value or text $formula = "%0".$length."s"; return sprintf($formula,$data); } else { $input_text = $data; $fixer = str_repeat("\x20", strval($length - strlen($input_text))); $output_text = $input_text . $fixer; return $output_text; } } ?> <form action="" method="post"> Text<input name="txt" type="text" /> Length<input name="txtLength" type="text" /> <input name="<?php $_SERVER['PHP_SELF']; ?>" type="submit" /> </form> Save this in one php file. ---- Server IP: 69.147.83.197 Probable Submitter: 202.152.81.227 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/107470 Del: integrated -- https://master.php.net/note/delete/107470/integrated Del: useless -- https://master.php.net/note/delete/107470/useless Del: bad code -- https://master.php.net/note/delete/107470/bad+code Del: spam -- https://master.php.net/note/delete/107470/spam Del: non-english -- https://master.php.net/note/delete/107470/non-english Del: in docs -- https://master.php.net/note/delete/107470/in+docs Del: other reasons-- https://master.php.net/note/delete/107470 Reject -- https://master.php.net/note/reject/107470 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186064) next »