Grandy Grandy - 1 year ago 132
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download