21

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

1
  • 3
    $password="1bsdf4"; $formatted = implode(' ', str_split($password)); echo $formatted; Commented Nov 27, 2013 at 15:56

6 Answers 6

43

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.

Sign up to request clarification or add additional context in comments.

Comments

11

You can use chunk_split() for this purpose.

$formatted = trim(chunk_split($password, 1, ' '));

trim is necessary here to remove the whitespace after the last character.

2 Comments

This should be the accepted answer since chunk_split() takes about 40% less time to compute than the implode(str_split()) method marked as "accepted". *(tested on 3100 cases with PHP 7.3.25)
I agree this is way faster (more like 70% faster by my benchmark), but the accepted answer still only took .9 seconds on 1 million 32 character strings and to me it's a bit more readable.
1

You can use this code [DEMO]:

<?php
 $password="1bsdf4";
 echo chunk_split($password, 1, ' ');

chunk_split() is build-in PHP function for splitting string into smaller chunks.

1 Comment

The only issue for this solution is that adds an extra space at the end of the generated string.
0

How about this one

$formatted = preg_replace("/(.)/i", "\${1} ", $formatted);

according to: http://bytes.com/topic/php/answers/882781-add-whitespace-between-letters

1 Comment

Though it works, regexes are a bit slow, alternative methods with native string functions are better
0

This also Worked..

$password="1bsdf4";    
echo $newtext = wordwrap($password, 1, "\n", true);

Output: "1 b s d f 4"

Comments

0
    function break_string($string,  $group = 1, $delimeter = ' ', $reverse = true){
            $string_length = strlen($string);
            $new_string = [];
            while($string_length > 0){
                if($reverse) {
                    array_unshift($new_string, substr($string, $group*(-1)));
                }else{
                    array_unshift($new_string, substr($string, $group));
                }
                $string = substr($string, 0, ($string_length - $group));
                $string_length = $string_length - $group;
            }
            $result = '';
            foreach($new_string as $substr){
                $result.= $substr.$delimeter;
            }
            return trim($result, " ");
        }

$password="1bsdf4";
$result1 = break_string($password);
echo $result1;
Output: 1 b s d f 4;
$result2 = break_string($password, 2);
echo $result2;
Output: 1b sd f4.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.