- rtrim: documentation ( source)
- trim: documentation ( source)
- preg_replace: documentation ( source)
<?php
$old = 'james,john,mark,luke,';
$users = 'james,john,mark,luke,mary,david';
$expr = rtrim(preg_replace('/\,/', '|', $old), '|');
$newstr = preg_replace("/$expr/i", '', $users);
echo trim($newstr, ',');