note 51219 added to function.number-format
| From: | ChronoFish at osu1 dot php dot net | Date: | Wed, 23 Mar 2005 19:14:32 +0000 |
| Subject: | note 51219 added to function.number-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86857@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.number-format.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51219
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51219&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51219&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51219&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51219&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51219&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51219&report=yes
Search -- http://master.php.net/manage/user-notes.php