Himanshu Chawla Himanshu Chawla - 8 months ago 23
PHP Question

How to add a space after every character in a string in php?

I have a string in php named $password="1bsdf4";

I want output "1 b s d f 4"

How is it possible. I was trying implode function but i was not able to do..

$formatted = implode(' ',$password);
echo $formatted;

I tried this code:

$formatted = implode(' ',$str);
echo $formatted;

Its working and adding space in hello and user !
Final Output I got Hello User

Thanks, yours answers will be appreciated.. :)


You can use implode you just need to use str_split first which converts the string to an array:

$formatted = implode(' ',str_split($password)); 


Sorry didn't see your comment @MarkBaker if you want to convert you comment to an answer I can remove this.