PHP concat small words in string with preg_replace

260 Views Asked by At

In efforts to normalize text for fuzzy matching I am trying to combine single letter words (mostly parts of an initialism) into a single word. Numbers are ignored. Below is where I'm at:

$strings = array("CROWN ROYAL X R CANADIAN WHISKEY", "CROWN ROYAL X R", "REMY V S O P 8 0", "BARTON L I I T");
$string = preg_replace('/\b([A-Z]){1}\s([A-Z]){1}\b/', '\1\2', $string);

Results:

"CROWN ROYAL XR CANADIAN WHISKEY", "CROWN ROYAL XR", "REMY VS OP 8 0", "BARTON LI IT"

I need VSOP and LIIT. I have implemented a workaround by using $string = preg_replace('/\b([A-Z]){1}\s([A-Z]){1}([A-Z]){1}\s([A-Z]){1}\b/', '\1\2\3\4', $string); first, but I'm certain there is a more graceful and less costly solution?

1

There are 1 best solutions below

0
On

check this

$strings = preg_replace('~(?<=\b[A-Z])\s+(?=[A-Z]\b)~', '', $strings);