我如何在 PHP 中与 str_replace 一起使用多个“-”

How can i aa multi "-" with str_replace in PHP

我的 str 是:

999999999

现在,如何在这个字符串中添加“-”?

我用“-”的例子:

99-9-9-99999

试试这个代码 -

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

输出 - 99-9-9-99999

您可以使用 preg_replace() 格式化数字。

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

输出:

99-9-9-99999