<?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);