Reputation: 3414
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.
Upvotes: 1
Views: 1982
Reputation: 20456
Split on non-word characters:
$array=preg_split("/\W+/", $string);
Upvotes: 1
Reputation: 816502
$words = preg_split('/[,;\s]+/', $str, -1, PREG_SPLIT_NO_EMPTY);
[,;\s]
is a character group which means match any of the characters contained in this group.\s
matches any white space character (space, tab, newline, etc.). If this is too much just replace it with a space: [,; ]
.+
means match one or more of the preceding symbol or group.http://www.regular-expressions.info/ is a good site to learn regular expressions.
Upvotes: 8
Reputation: 57167
You want to use preg_split and use [;, ]+ for your regex to split on
$keywords = preg_split("/[;, ]+/", $yourstring);
Upvotes: 3