3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Mauris commodo quis imperdiet massa. Turpis egestas pretium aenean pharetra. In hac habitasse platea dictumst ves33tibulum rhoncus est pellentesque. Purus faucibus ornare suspendisse sed nisi lacus. Malesuada bibendum arcu vitae elementum curabitur vitae nunc sed velit. Aliquam ultrices sagittis orci a scelerisque. Lorem123 ipsum dolor sit amet consectetur adipiscing. Tristique senectus et netus et malesuada fames ac. Eget duis at tellus at urna. Auctor eu augue ut lectus arcu bibendum at. In ante metus dictum at tempor com444modo ullamcorper a. Dolor magna eget est lorem ipsum. Lorem dolor sed viverra ipsum nunc aliqu666et bibendum. Urna duis convallis convallis tellus id."; $pattern = # Word before a comma # -- Positive look ahead assertion '%\w+(?=,)' # All Characters inbetween up to the next comma # -- Negative look after assertion .'.*(?<=,)%'; preg_match($pattern, $string, $match); var_dump($match); $pattern = # Get all words that contain the letter p and are just before a full stop. '%(\w+p\w+)(?=\.)%'; ; # Match will contain a subgroup match, notice the extra returned value in array. preg_match($pattern, $string, $match); var_dump($match); #### Flags # PREG_OFFSET_CAPTURE # -- Return the offset from each result given in match - integer for location in string preg_match($pattern, $string, $match, PREG_OFFSET_CAPTURE); var_dump($match); $pattern = # Find the first mixed number and letter word. # Remove unfound subpatterns with null with optional flag '%\w+\d+(999)*\w+%'; # PREG_UNMATCHED_AS_NULL preg_match($pattern, $string, $match, PREG_UNMATCHED_AS_NULL); var_dump($match); # Offset - Assertions can make results differ preg_match($pattern, $string, $match, PREG_OFFSET_CAPTURE, 250); var_dump($match);
based on Jak61
Output for 7.4.0 - 7.4.12, 8.0.0
array(1) { [0]=> string(34) "amet, consectetur adipiscing elit," } array(2) { [0]=> string(10) "adipiscing" [1]=> string(10) "adipiscing" } array(2) { [0]=> array(2) { [0]=> string(10) "adipiscing" [1]=> int(483) } [1]=> array(2) { [0]=> string(10) "adipiscing" [1]=> int(483) } } array(2) { [0]=> string(12) "ves33tibulum" [1]=> NULL } array(1) { [0]=> array(2) { [0]=> string(8) "Lorem123" [1]=> int(441) } }
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.23
array(1) { [0]=> string(34) "amet, consectetur adipiscing elit," } array(2) { [0]=> string(10) "adipiscing" [1]=> string(10) "adipiscing" } array(2) { [0]=> array(2) { [0]=> string(10) "adipiscing" [1]=> int(483) } [1]=> array(2) { [0]=> string(10) "adipiscing" [1]=> int(483) } } array(1) { [0]=> string(12) "ves33tibulum" } array(1) { [0]=> array(2) { [0]=> string(8) "Lorem123" [1]=> int(441) } }
Output for 7.1.25 - 7.1.33
array(1) { [0]=> string(34) "amet, consectetur adipiscing elit," } array(2) { [0]=> string(10) "adipiscing" [1]=> string(10) "adipiscing" } array(2) { [0]=> array(2) { [0]=> string(10) "adipiscing" [1]=> int(483) } [1]=> array(2) { [0]=> string(10) "adipiscing" [1]=> int(483) } } Notice: Use of undefined constant PREG_UNMATCHED_AS_NULL - assumed 'PREG_UNMATCHED_AS_NULL' in /in/Y0JWg on line 37 Warning: preg_match() expects parameter 4 to be integer, string given in /in/Y0JWg on line 37 array(2) { [0]=> array(2) { [0]=> string(10) "adipiscing" [1]=> int(483) } [1]=> array(2) { [0]=> string(10) "adipiscing" [1]=> int(483) } } array(1) { [0]=> array(2) { [0]=> string(8) "Lorem123" [1]=> int(441) } }