Regular expression in PHP

1

Good night, I need to receive a string via POST, and break it into keywords, but it is possible for the user to separate words in three ways: comma (','), empty space or comma followed by empty space (',').
I'm thinking of using the split () method, 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).

    
asked by anonymous 04.07.2015 / 02:41

1 answer

1

For this you should use preg_split , example:

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

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