-5

my str is :

999999999

Now, how can i add "-" in this str ?

my example with "-" :

99-9-9-99999
MNamazi
  • 101
  • 1
  • 14

2 Answers2

2

Try this code -

    <?php
       $str = "999999999"; 
       $output =  substr($str,1,2)."-".substr($str,2,1)."-".substr($str,3,1)."-".substr($str,4,5);
       echo "$output";
    ?>

output - 99-9-9-99999

Rashed
  • 2,349
  • 11
  • 26
0

You can use preg_replace() to format the number.

<?php
    $num = "999999999";
    $new_num = preg_replace("/([0-9]{2})([0-9]{1})([0-9]{1})([0-9]{5})/", "$1-$2-$3-$4", $num);
    echo $new_num;
?>

Output:

99-9-9-99999

Greg Klaus
  • 109
  • 5