I've got a PHP coded version already, I was just looking at the interest in
having this part of the language.
My approach is more simplistic, I've never had a problem with it.
function mprint($mask, $value, $mask_char = "X")
{
if (empty($value))
return false;
$retval = ""; // init return value
$vp = 0; // value position
for ($i=0; $i<strlen($mask); $i++) {
if ($mask[$i] == $mask_char) {
// append char to retval
$retval .= $value[$vp];
$vp++; // increment position
} else {
$retval .= $mask[$i]; // don't mess with this char
}
}
if ($vp != strlen($value))
$retval .= substr($value,$vp,strlen($value)); // append left-overs
return $retval;
} /* end of function mprint() */
-Chris
-----Original Message-----
From: Joe Brown [mailto:[EMAIL PROTECTED]]
Sent: Monday, 09 April, 2001 2-44 pM
To: [EMAIL PROTECTED]
Subject: Re: [PHP-DEV] Would anybody find this useful besides me?
I wrote a little function to do something similar to that.
It's a little weak but does a fairly good job formatting. Maybe sombody
will make it better and send me the code too ;-)
// format($element, $format)
// element=<input ...>
// format="(999)999-9999 xxxxx"
// 9=numeric digit
// X=converts alpha to uppercase
// x=anything goes
// anything-else=hardcoded format literals.
// above format would take 1234567890 and conver it to (123)456-7890
// amazingly format="99/99/9999" doesn't add 0's but does convert 01012000
to
// 01/01/2000 it also accepts 1/1/2000 and leaves it as 1/1/2000
// but "999-99-9999" truncates 333-2-24444 to 333-2-2444
// Use:
// <form>
// <input type=text name=f1
onchange="this.value=inputFormat(this.value,'99/99/9999')">
function format ($e,$f) {
if (($e=='') || ($f=='')) return $e;
$r="";
$numbers='0123456789';
$ei=0;
$fi=0;
while(strcmp($ev=substr($e,$ei,1),"") && strcmp($fv=substr($f,$fi,1),""))
{
switch($fv) {
case '9':
if (strpos($numbers,$ev)!==false) {
$r.=$ev;
$ei++;
$fi++;
}
else { // else clause causes 1/1/1999 to be accepted as such
if (substr($f,$fi+1,1)==$ev)
$fi++;
else
$ei++;
}
break;
case 'X':
$r.=strtoupper($ev);
$ei++;
$fi++;
break;
case 'x':
$r.=$ev;
$ei++;
$fi++;
break;
default:
if ($ev==$fv){
$r.=$fv;
$ei++;
$fi++;
}
else {
$r.=$fv;
$fi++;
break;
}
}
}
return $r;
}
""Chris Newbill"" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> Just curious to see if anybody else would see the usefulness of having a
> masking print function? I dunno maybe call it mprint().
>
> Where I could pass my mask, my value and optionally the mask char,
> defaulting to something off I guess like "~".
>
> mprint ( mask , value [, mask_char])
>
> So I could
>
> $str = mprint("(XXX) XXX.XXXX", "4064498056", "X");
> print $str;
>
> and would get
>
> (406) 449.8056
>
>
> Chris Newbill
> OneWest.net Inc.,
> Programmer/Analyst
>
> 406.449.8056
> [EMAIL PROTECTED]
>
> /*
> Windows crashed.
> I am the Blue Screen of Death.
> No one hears your screams.
> */
>
>
>
>
> --
> PHP Development Mailing List <http://www.php.net/>
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>
--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]
--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]