Re: How to format a strign with a generic mask??

From: Date: Thu, 12 Jun 2003 01:41:08 +0000
Subject: Re: How to format a strign with a generic mask??
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-151181@lists.php.net to get a copy of this message
"Felipe Desiderati" <felipe@unimedrj.com.br> wrote in message news:003301c33078$db569840$af00000a@fdrj.unimed... >I need a simple function that takes a string and formats > with a generic mask. Like this example above: > >$string1 = "12345678" >$string2 = "11062003" > >echo format_string ("#.###.###-#", $string1); // --> Returns 1.234.567-8 >echo format_string ("##/##/####", $string1); // --> Returns 11/06/2003 > >Does anybody here have an idea to how to do this? Here is a function that does what you want, with a test stand. HTH! <?php function format_string($mask, $contents, $ch = '#') { $c = 0; $str = ""; for($m = 0; $m < strlen($mask); $m++) if ($mask[$m] == $ch) { $str .= $contents[$c]; $c++; } else $str .= $mask[$m]; return($str); } ?> <html> <body> <form action="mask.php" method="get"> Mask: <input type="text" name="mask" value="<?php echo $_GET['mask']; ?>"/><br/> Content: <input type="text" name="cont" value="<?php echo $_GET['cont']; ?>"/><br/> Maskchar: <input type="text" name="ch" value="<?php echo $_GET['ch']; ?>"/><br/> <input type="submit"> </form> <b>Result:</b> <?php echo format_string($_GET['mask'], $_GET['cont'], $_GET['ch']); ?><br/> </body> </html> -- Hugh Bothwell hugh_bothwell@hotmail.com Kingston ON Canada v3.1 GCS/E/AT d- s+: a- C+++ L++>+++$ P+ E- W+++$ N++ K? w++ M PS+ PE++ Y+ PGP+ t-- 5++ !X R+ tv b++++ DI+++ D-(++) G+ e(++) h-- r- y+

« previous php.general (#151181) next »