PHP: Making Credit Card Numbers Human-Readable

If you’ve ever tried to enter a 16 digit credit card number all in one blob, you’ll know it’s hard. To make life easier for the person responsible for processing online credit card orders, I needed to split up the credit card number and insert a space every 4 characters.

I’m not very schooled up on RegEx, and after spending ages searching for how to do it, I came across this:

echo implode(' ',str_split($credit_card_number,4));

Hopefully it saves someone else some time!

By Phil Wiffen

Phil is an IT Professional working in Cambridge, England. He generally blogs about useful solutions that he comes across in his work/play.

One reply on “PHP: Making Credit Card Numbers Human-Readable”

Leave a Reply

Your e-mail address will not be published. Required fields are marked *