<?php $str = 'test, test, test2, test2, test3';echo preg_replace('/\b(\w+),\s+\1\b/', '$1', $str);
Standard input is empty
test, test2, test3
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!