"Felipe Desiderati" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>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     [EMAIL PROTECTED]     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+




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to