phpregexpreg-split

Separating words (with spaces and specific punctuation in between) by using regex in PHP


I have a string and it contains some words that I want to reach, seperators can be any string that consist of , ; or a space.

Here is a example:

;,osman,ali;, mehmet ;ahmet,ayse; ,

I need to take words osman ali mehmet ahmet and ayse to an array or any type that I can use them one by one. I tried it by using preg function but i couldn't figure out.

If anyone help, I will be appreciative.


Solution

  • $words = preg_split('/[,;\s]+/', $str, -1, PREG_SPLIT_NO_EMPTY);
    

    DEMO

    http://www.regular-expressions.info/ is a good site to learn regular expressions.