我想以XXX-XXX-XXXX格式显示电话号码。 我尝试了很多方法,但无法得到正确的答案

I want to display phone number in this format XXX-XXX-XXXX. But now it's displaying like this XXX-XXX-XXX-X

<div class="row mb15">
    <div class="col-12 col-md-5">
        <p>Home Phone</p>
    </div>
    <div class="col-12 col-md-7 phone-number">
        <h4><?php echo join('-', str_split($candidateDet['homephone'], 3)); ?></h4>
    </div>
</div>

Alternative ideas are welcome

You could use preg_replace() to find and group the numbers.

Something like:

echo preg_replace('#(\d{3})(\d{3})(\d{4})#', '$1-$2-$3', $phoneNumber);

An example.