I'm trying to retrieve 5 words before after a particular word using regex. My code is as follows.
$str= '<li>111-37774 Blue</li><li>111-1566 Red</li><li>122-4555 White</li><li>1455-789 Yellow</li></ul>Minimum order applies. This is a string for testing.<p>';
$regexForPattern ='/((?:\w+\W\s*){0,5})minimum\b((?:\W*\w+){0,5})/i';
preg_match_all ($regexForPattern , trim( preg_replace('#<[^>]+>#', ' ', $str) ), $patternMatches);
print_r($patternMatches);
I want 5 words before and after the word 'minimum' from $str
.
Currently I'm getting the output as:
Array ( [0] =>
Array ( [0] => 4555 White 1455-789 Yellow Minimum order applies. This is a )
[1] => Array ( [0] => 4555 White 1455-789 Yellow )
[2] => Array ( [0] => order applies. This is a )
)
I expect the string 122-4555 White 1455-789 Yellow instead of 4555 White 1455-789 Yellow in the resultant array. For the words like 1455-789 it is considering 1455 as one word and 789 as another. How can I get only exact words?
Can anyone help me to fix this? Thanks in advance.