note 107470 deleted from function.preg-match by tularis
| From: | tularis@php.net | Date: | Sun, 12 Feb 2012 11:33:54 +0000 |
| Subject: | note 107470 deleted from function.preg-match by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-186084@lists.php.net to get a copy of this message | ||
Note Submitter: decastroarman at hotmail dot com
----
<?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.