0

I am want to format a text from using a string as the formatter. Something like this:

echo formatText('5555555555','(ddd) ddd-dddd');

which will give me result

(555) 555-5555

How can I achieve that?

Thanks, Gasim

2
  • Check the function money_format(). Commented Sep 30, 2013 at 11:18
  • Sounds like a good practice using money_format() to format a telephone number! :D Commented Sep 30, 2013 at 11:20

2 Answers 2

1

One way would be to use preg_replace to match valid input and format it as desired:

echo preg_replace('/^(\d{3})(\d{3})(\d{4})$/', '($1) $2-$3', '5556667777');
Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at sprintf I think it's what you're looking for

echo sprintf("(%d) %d-%d",$prefix, $first-part, $second-part);

or

printf("(%d) %d-%d",$prefix, $first-part, $second-part);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.