do you know how can i put a hyphen (-) after 3 digits of a number(starting from the end) in php? example: the number 9787897878 to be shown as 978-789-7878??

Recommended Answers

Something like this:

$subject = '9787897878';
$result = preg_replace('/(\d{3})(\d{3})(\d*)/i', '\1-\2-\3', $subject);
echo $result;
Jump to Post

All 2 Replies

Something like this:

$subject = '9787897878';
$result = preg_replace('/(\d{3})(\d{3})(\d*)/i', '\1-\2-\3', $subject);
echo $result;

preg_replace("/^(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $phone):'')

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.