2

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.

Jenz
  • 8,280
  • 7
  • 44
  • 77

1 Answers1

1

The \w cannot match - between the digits, hence the regex fails to fetch the expected substring from the expected location.

You should replace (?:\w+\W\s*){0,5} with (?:\S+\s+){0,5} and (?:\W*\w+){0,5} with (?:\s+\S+){0,5}:

'~((?:\S+\s+){0,5})minimum\b((?:\s+\S+){0,5})~'

See the regex demo.

That way, you will match any 0 to 5 space-separated non-whitespace chunks before and after the keyword.

See the PHP demo:

$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 ='/((?:\S+\s+){0,5})minimum\b((?:\s+\S+){0,5})/i';   
$nstr = trim( preg_replace('#<[^>]+>#', ' ', $str));
echo $nstr . "\n";
preg_match_all ($regexForPattern , $nstr, $patternMatches); 
print_r($patternMatches);
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563