开发者

php separating words by using regex

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.


$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.

DEMO

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


You want to use preg_split and use [;, ]+ for your regex to split on

$keywords = preg_split("/[;, ]+/", $yourstring);


Split on non-word characters:

$array=preg_split("/\W+/", $string);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜