Php Preg_Split() Function

Can anybody explain khổng lồ me how to use preg_split() function?I didn"t understand the pattern parameter lượt thích this "/+/".

Bạn đang xem: Php preg_split() function

for example:

I have this subject: is is. And I want the results to be:

array ( 0 => "is", 1 => "is",)so it will ignore the space and the full-stop, how I can vì that?



preg means Pcre REGexp", which is kind of redundant, since the "PCRE" means "Perl Compatible Regexp".

Regexps are a nightmare to the beginner. I still don’t fully understand them và I’ve been working with them for years.

Xem thêm: 60 Năm Tác Phẩm Sửa Đổi Lối Làm Việc Pdf, Download Tài Liệu Sửa Đổi Lối Làm Việc

Basically the example you have there, broken down is:

"/+/"/ = start or over of pattern string< ... > = grouping of characters+ = one or more of the preceeding character or groups = Any whitespace character (space, tab)., = the literal comma characterSo you have a search pattern that is "split on any part of the string that is at least one whitespace character and/or one or more commas".

Other common characters are:

. = any single character* = any number of the preceeding character or group^ (at start of pattern) = The start of the string$ (at kết thúc of pattern) = The over of the string^ (inside <...>) = "NOT" the following characterFor PHP there is good information in the official documentation.

cốt truyện
edited Jun 12, 2014 at 17:15
answered Jun 12, năm trước at 16:50

1,7501515 silver badges2626 bronze badges
địa chỉ a phản hồi |
This should work:

$words = preg_split("/(?";print_r($words);echo "";The đầu ra would be:

Array( <0> => is <1> => is)Before I explain the regex, just an explanation on PREG_SPLIT_NO_EMPTY. That basically means only return the results of preg_split if the results are not empty. This assures you the data returned in the array $words truly has data in it & not just empty values which can happen when dealing with regex patterns & mixed data sources.

And the explanation of that regex can be broken down like this using this tool:

NODE EXPLANATION-------------------------------------------------------------------------------- (?An nicer explanation can be found by entering the full regex pattern of /(? in this other other tool:

(? Positive Lookbehind - Assert that the regex below can be matchedw match any word character  assert position at a word boundary (^w|w$|Ww|wW)s* match any trắng space character < f >Quantifier: Between zero và unlimited times, as many times as possible, giving back as needed !?. A single character in the danh mục !?. Literally

That last regex explanation can be boiled down by a human—also known as me—as the following:

Match—and split—any word character that comes before a word boundary that can have multiple spaces và the punctuation marks of !?..