note 50327 added to function.substr
| From: | MatiasfromArgentina at osu1 dot php dot net | Date: | Thu, 24 Feb 2005 20:55:52 +0000 |
| Subject: | note 50327 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85502@lists.php.net to get a copy of this message | ||
Hello,
Here you are a function to format your
numeric strings. Enjoy it.
<?php
function str_format_number($String, $Format){
if ($Format == '') return $String;
if ($String == '') return $String;
$Result = '';
$FormatPos = 0;
$StringPos = 0;
While ((strlen($Format) - 1) >= $FormatPos){
//If its a number => stores it
if (is_numeric(substr($Format, $FormatPos, 1))){
$Result .= substr($String, $StringPos, 1);
$StringPos++;
//If it is not a number => stores the caracter
} Else {
$Result .= substr($Format, $FormatPos, 1);
}
//Next caracter at the mask.
$FormatPos++;
}
return $Result;
}
// For phone numbers at Buenos Aires, Argentina
// Example 1:
$String = "541143165500";
$Format = "+00 00 0000.000";
Echo str_format_number($String, $Format); // Returns "+54 11 4316.5500"
// Example 2:
$String = "541143165500";
$Format = "+00 00 0000.0000000";
Echo str_format_number($String, $Format); // Returns "+54 11 4316.5500"
// Example 3:
$String = "541143165500";
$Format = "+00 00 0000.000 a";
Echo str_format_number($String, $Format); // Returns "+54 11 4316.550 a"
?>
How it works explanation:
str_format_number($String, $Format)
Spects two parameters $String and $Format,
both should be strings.
$String: coulbe any kind of data type,
but it's oriented to numeric string, like
phone numbers.
$Format: should be a conjunction between
numbers (any one) and others caracters.
str_format_number takes each caracter
of $Format, if it isn't a number stores
it to be returned later, but if it is a
number takes the caracter of $String
placed in the position corresponding to
the amount of numbers in $Format so far
starting from zero.
If $Format has less numbers than $string
caracters the rest of the caracters at
the end of $String should be ignored.
If $Format has more numbers than $string
caracters the no caracter will be used,
so those will be ignored.
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50327
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50327&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50327&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50327&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50327&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50327&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50327&report=yes
Search -- http://master.php.net/manage/user-notes.php