Madmenyo
Madmenyo

Reputation: 8584

Make a multiple delimiter REGEX for preg_split

I need to split multiple lines in multiple files by different delimiters. I think preg_split should do the job but i never worked with PCRE REGEX stuff. I could also change all my delimiters to be consistent but that adds unnecessary calculations.

Q: My delimiters consist of (,)(;)(|)(space) and i am curious how to build such a REGEX.

Upvotes: 3

Views: 6713

Answers (2)

picios
picios

Reputation: 250

Try this:

$string = "foo:bar|it;is:simple";
print_r(preg_split ( '/,|;|\||\s/' , $string ));

Upvotes: 3

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

Put the characters in square brackets []:

$parts = preg_split('/[,;| ]/', $string, null, PREG_SPLIT_NO_EMPTY);

You can also use \s instead of a space character, which matches all kinds of whitspace, such as tabs and newlines.

Upvotes: 10

Related Questions