PHP str_replace to replace numbers with words

PHPstr-replace

I have a website that needs to display phone numbers to users, but rather than display the actual number for someone to harvest from the source code, I'd like to replace each digit of the phone number with the corresponding word. For instance…

355-758-0384

Would become

three five five - seven five eight - zero three eight four

Can this be done? Sorry, I don't have any code to display because I don't even know where to start with this.

Best Answer

$string="355-758-0384";
$search  = array(0,1,2,3,4,5,6,7,8,9);
$replace = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
echo str_replace($search, $replace, $string);

Then you can use any different string aswell without having to update code

Edit:

And if you want automatic spaces on the end then your $replace array can be

$replace = array('zero ', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ');