note 51219 deleted from function.number-format by levim
| From: | levim@php.net | Date: | Sun, 23 Mar 2014 14:13:27 +0000 |
| Subject: | note 51219 deleted from function.number-format by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199021@lists.php.net to get a copy of this message | ||
Note Submitter: ChronoFish
----
I was looking for an easy way to take a number (or string) and force into a specific format. I came
up with. I apologize if this is redundant, but I could not find a simular function:
/***********************************
* string_format
***********************************/
function string_format($format, $string, $placeHolder = "#")
{
$numMatches = preg_match_all("/($placeHolder+)/", $format, $matches);
foreach ($matches[0] as $match)
{
$matchLen = strlen($match);
$format = preg_replace("/$placeHolder+/", substr($string, 0, $matchLen), $format, 1);
$string = substr($string, $matchLen);
}
return $format;
}
To Use:
print string_format("(###)###-####", "4015551212");
will print out:
(401)555-1212
Hope this helps someone,
CF