Handling Dots and Spaces in preg_split()
When you need to tokenize a string by specific delimiters, PHP's preg_split() is often the go-to tool because it allows you to define the splitting logic via regular expressions. However, the behavior changes significantly depending on whether you use character classes or alternation, and how you handle optional whitespace around those characters.
Consider the input string 'is is.'. If your goal is to split this into ['is', 'is'], effectively ignoring both the space between words and the trailing full-stop, you need a pattern that matches these characters but does not include them in the resulting tokens. The core challenge lies in ensuring the regex engine treats the delimiter strictly as a boundary rather than part of the data.
Constructing the Regular Expression
To split on spaces and dots, you must define a character class or an alternation that encompasses both (space) and . (dot). A common mistake is attempting to use [\ .], which works correctly in many contexts but can be finicky regarding escaping within square brackets. Inside a character class defined by [...], the dot usually retains its literal meaning, so escaping it with a backslash (\.) is technically redundant but harmless. However, relying on redundancy can obscure intent.
A more robust approach for this specific requirement is using alternation with the | operator or a concise character class. The pattern /[\s\.]/ covers spaces (using \s) and dots. While this works, it splits strictly on the presence of these characters. If you apply preg_split('/[\s\.]/', 'is is.'), PHP returns ['is', '', 'is']. Notice the empty string between the first dot-space sequence? Actually, in your specific case of 'is is.', there is a space after the first word and a dot at the end.
Let's refine the logic based on the desired output ['is', 'is']. The input has a space between "is" and "is", and a dot after the second "is".
- Splitting on the space yields:
'is','is.' - Splitting on the dot yields:
''(empty),'is'
The issue is that simply splitting creates empty strings if the delimiter appears at the very end of the string or consecutively. To get clean tokens, you generally want to strip the delimiters entirely from the result. preg_split() handles this by default; it removes the matched delimiters and returns the remaining substrings.
However, if your input is strictly 'is is.', splitting on /[\s\.]/ produces:
- Before space:
'is' - Between space and dot:
''(empty string) - After dot:
''(if trailing delimiter creates an empty element depending on the flags)
Wait, let's trace 'is is.' with /[\s\.]/:
- Match space after first "is": Result so far
['is']. Remaining:'is.' - Match dot after second "is": Result so far
['is', 'is']. Remaining:'' - Does the trailing dot create an empty element? Yes, by default.
To avoid that trailing empty string, you must ensure your pattern doesn't match the final delimiter as a split point that generates an empty tail, or better yet, use the PREG_SPLIT_NO_EMPTY flag if you plan to filter later, but the cleanest solution for tokenization where punctuation is purely a separator is to rely on preg_split's default behavior and simply ignore trailing empty elements if they occur, or adjust the pattern.
Actually, looking closely at 'is is.':
- Split 1 (space):
'is','is.' - Split 2 (dot in
'is.'):'is',''
So the raw result is ['is', 'is', '']. To get exactly ['is', 'is'], you have two options:
- Use
preg_split('/[\s\.]+/', ...)? No, that doesn't change the trailing empty string behavior for a single trailing delimiter. - The most idiomatic fix is to use
preg_split('/[\s\.]+/', $string, -1, PREG_SPLIT_NO_EMPTY). This flag filters out any empty strings resulting from consecutive delimiters or leading/trailing delimiters.
Implementation in PHP
Here is the precise implementation to achieve the desired output:
$string = 'is is.';
$pattern = '/[\s\.]+/'; // Matches one or more spaces or dots consecutively
$options = PREG_SPLIT_NO_EMPTY;
$parts = preg_split($pattern, $string, -1, $options);
print_r($parts);
// Output: Array ( [0] => 'is' [1] => 'is' )
Why This Works
Using the + quantifier ([\s\.]+) groups consecutive delimiters into a single split point. While not strictly necessary for ' is . ' vs 'is is.', it makes the regex more resilient to inputs like 'is is.. '.
Crucially, the PREG_SPLIT_NO_EMPTY flag is the key here. Without it, preg_split() retains empty strings between delimiters or at the ends of the string when a delimiter matches but leaves nothing behind. For example, splitting 'a.b' on /./ yields ['a', 'b']. Splitting 'a.' on /./ yields ['a', '']. By appending the flag, you tell PHP to discard those empty entries automatically.
Trade-offs and Considerations
Using [\s\.]+ effectively treats spaces and dots as whitespace-like separators. If your data contains words like hello.world, this approach will split them into ['hello', 'world']. If you need to preserve hyphenated or dot-connected identifiers (like version numbers v1.0), this pattern is too aggressive. In those cases, you would need a negative lookbehind/lookahead or a more specific character class that excludes dots when they are adjacent to alphanumerics in specific ways.
However, for simple tokenization where punctuation and whitespace serve solely as boundaries, the combination of a broad character class and the PREG_SPLIT_NO_EMPTY flag is the standard, efficient solution. It avoids the overhead of multiple passes or post-processing loops to trim empty elements manually.