I have a sting like "03335240141" i want display it like 0333-524-0141 is there any string function that can do this for me?
thank you
dizyn
a small string question
Moderator: General Moderators
- Sekka
- Forum Commoner
- Posts: 91
- Joined: Mon Feb 18, 2008 10:25 am
- Location: Huddersfield, West Yorkshire, UK
Re: a small string question
There's no built in function for formatting strings like that, however, this should do the trick,
Code: Select all
function format ($str) {
// Validate
if (!is_string ($str) || strlen ($str) != 11) {
return false;
}
// Format
return substr ($str, 0, 4) . "-" . substr ($str, 4, 3) . "-" . substr ($str, 7);
}Re: a small string question
that is if the dash is ALWAYS in the same spot though. So be sure.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: a small string question
Assuming actually wanted the 2nd octet to only be 3 characters,
xxxx-xxx-xxxx format
xxxx-xxxx-xxx format (which is what I'd expect personally)
xxxx-xxx-xxxx format
Code: Select all
function formatID ($id)
{
return preg_replace('#(\d{4})(\d{3})(\d{4})#', '$1-$2-$3', $id);
}Code: Select all
function formatID2($id)
{
return implode('-', str_split($id, 4));
}