When you need to add character(s) after every n number of characters in a string, you can use the following function to do so. This function will be useful especially when you have something like a credit card number like:
5455471516335477
This function will convert this value to:
5455-4715-1633-5477
or
5455 4715 1633 5477
function addchar($str, $padding_at, $padding_char){
$arr = str_split($str, $padding_at);
$str=implode($padding_char, $arr);
}
Example usage:
$str='5455471516335477';
$str=addchar($str, 4, '-');
echo $str; // output will be 5455-4715-1633-5477
5455471516335477
This function will convert this value to:
5455-4715-1633-5477
or
5455 4715 1633 5477
function addchar($str, $padding_at, $padding_char){
$arr = str_split($str, $padding_at);
$str=implode($padding_char, $arr);
}
Example usage:
$str='5455471516335477';
$str=addchar($str, 4, '-');
echo $str; // output will be 5455-4715-1633-5477
No comments:
Post a Comment