PHP 8.5.0 Alpha 2 available for testing

Voting

: max(three, six)?
(Example: nine)

The Note You're Voting On

mojo
4 years ago
Why <?php preg_match_all('/(?:^|\s)(ABC|XYZ)(?:\s|$)/i', 'ABC XYZ', $match) ?> finds only 'ABC'?

Because the first full match is 'ABC ' - containing the trailing space. And that space is not available for further processing.

Use lookbehind and lookahead to solve this problem: <?php preg_match_all('/(?<=^|\s)(ABC|XYZ)(?=\s|$)/i', 'ABC XYZ', $match) ?>

<< Back to user notes page

To Top