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?
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.
$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 characterThat 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 !?..