Search code examples
phpwhitespace

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..

$password="1bsdf4";    
$formatted = implode(' ',$password);    
echo $formatted;

I tried this code:

$str=array("Hello","User");    
$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.. :)


Solution

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

    $password="1bsdf4";    
    $formatted = implode(' ',str_split($password)); 
    

    http://www.php.net/manual/en/function.str-split.php

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