1

I want to separate a string with a whitespaces based on the pattern I want. In every two words and sometimes three. For example:

$string = 'marketplace';

become

$string = 'mark et pl ace';

I know that preg_replace can do this but I don't know the pattern. Can anybody tell me how to do it? Thanks

7
  • is it fix to two characters? Commented Aug 27, 2014 at 10:51
  • 5
    $string = implode(' ', str_split($string, 2)); Commented Aug 27, 2014 at 10:52
  • @Abaji, then billyonecan's anser is perfect for your need! Commented Aug 27, 2014 at 10:56
  • I think that right selection depends on rule where do you want to insert that whitespaces. If it's "dumb" (insert whitespace each 2 characters), you don't have to use regular expression. Commented Aug 27, 2014 at 10:57
  • @DawidFerenczy, that's give me another idea, so I edit my question. thanks. Commented Aug 27, 2014 at 11:01

5 Answers 5

5

If you want to use preg_replace...., however @billyonecan's str_split is probably a better way.

preg_replace('/(..)/','$1 ', $string);
Sign up to request clarification or add additional context in comments.

5 Comments

And maybe trim(...) for the last space.
Per OP requirement this should be accepted. Nevertheless, @vlzvl answer is correct too.
And if you want to do it every 'n' then: /(.{n})/ eg: /(.{3})/ also if the original string is an exact multiple of 'n' in length as Joop points out you probably want to rtrim() the result.
@Yorick, it's working. But what if I want to separate the first 3 characters and the next is 2?
@Abaij if you are looking for something like "MarketingStuff" => "Mar ke tin gS tuf f", ie: 3 characters then two characters etc you would use something like /(.{3})(.{2})/ with a replacement pattern of '$1 $2 '. That said, if you always want the same length of split throughout the string but different for different strings see my comment above. If you are varying the length within the string and especially if it is not in a repeating manner you may want to resort to a different approach.
4

this?

$string = 'market';
echo implode(" ",str_split($string,2));

3 Comments

your answer is the same to @billyonecan
indeed, just saw the comment.
@John Robertson, just saw the comment after wrote my answer.
1

This pattern should work with preg_replace:

$result = preg_replace("/(\\w{2})/uim", "$1 ", $string);

example:

http://regex101.com/r/hZ0xA1/1

Comments

1

Just use

implode(" ",str_split($string, 2))

Here the important code is

<?php
$string = "market";
echo implode(" ",str_split($string, 2));
?>

str_split converts the $string to an array which contents packs of 2 characters.

then implode will join the arrays with spaces b/w all array values.

Comments

0

Use this function. Just decide in which steps the spaces should be placed.

function split_by_position($string, $position){
    $splitted = str_split($string, $position);

    foreach($splitted as $part){
        $result .= $part.' ';
    }
    echo $result;
}

$string = 'market';
echo split_by_position($string, 2);

billyonecan's solution really looks better and for sure shorter. I suggest you to use his one. ^^

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.