Grandy Grandy - 7 months ago 45
PHP Question

PHP regex split by first two spaces

Heres my code right now:

preg_split( '/ {2}/', $searchString );

How should i set the {} that it is splitting the first two spaces and ignores the rest so:

foo foo foo foo


foo|foo|foo foo

EDIT: Because of this answer i recognized regex isn't even necessary. I didn't know that it is possible to limit explode.


You can apply limits to the number of replacements done with PREG functions.

To get what you need you could do the following:

$newString = preg_replace( '/\s+/', '|', 'foo foo foo foo', 2);

This will replace first 2 sets of white characters with a pipe.