I wrote a little function that also works with numbers of other lengths than 8:
<?php
function telephonize($number)
{
$return = array();
for ($i=0; $i<strlen($number); $i+=3)
{
In most cases, add the next 3 digits.
if (strlen($number)-$i != 4)
{
$return[] = substr($number, $i, 3);
}
If there are only four digits left, split them in 2&2 instead of 3&1.
Break out of the loop.
else
{
$return[] = substr($number, $i, 2);
$return[] = substr($number, $i+2, 2);
break;
}
}
# Turn the array elements into a string.
$return = implode(' ', $return);
return $return;
}
Examples:
echo telephonize(12345)."<br>";
echo telephonize(123456)."<br>";
echo telephonize(1234567)."<br>";
echo telephonize(12345678)."<br>";
echo telephonize(123456789)."<br>";
?>