Regular expression in PHP

Asked

Viewed 90 times

1

Good evening, I need to receive a string via POST, and break it into keywords, but it is possible that the user separates words in three ways: comma(','), empty space(' ') or comma followed by empty space(', ').
I’m thinking of using the method split(), since one of its parameters is a regular expression.
My question is: how would a regular expression consider these conditions? (comma OR empty space OR comma followed by empty space).

1 answer

1


For this you must use the preg_split, example:

$palavras = preg_split("/[\s,]+/", $_POST["string"]);

// reparte a frase quando encontrar espaço (\s), vírgula ou os dois

Browser other questions tagged

You are not signed in. Login or sign up in order to post.