Search code examples
phpsplitspaces

How to add a space between every sequence of four characters (like a credit card number)?


How to split this string :

56A19E6D77828

into format like this :

56A1 9E6D 7782 8

Are there any native PHP functions built for this?


Solution

  • You can use str_split()

    $card = "56A19E6D77828";
    echo join(" ", str_split($card, 4));
    

    Outputs:

    56A1 9E6D 7782 8