I have a string of ID's separated by commas and there is one vertical bar that separated the string into two sides. I need to be able to find an ID and remove it without effecting other numbers that may contain the ID, and without leaving two consecutive commas (the vertical bar needs to stay). I have a four step process already, but I would like to do this with one step if possible:
$possibilities = array(
'3,5,7|2,9,13', // 3 before a comma and at the beginning
'5,3,7|2,9,13', // 3 in-between commas
'5,7,3|2,9,13', // 3 in-between a comma and a vertical bar
'3|5,7,2,9,13', // 3 at the beginning and before a vertical bar
'5,7,13|3,2,9', // 3 after the vertical bar and before a comma
'5,7,2,9,13|3', // 3 after the vertical bar and at the end
'5,7,2,13|9,3', // 3 after a comma and at the end
);
$value = 3;
$results = preg_replace( "/(^|\\D){$value}(\\D|$)/", '$1$2', $array );
$results = preg_replace( '/(,\\|)|(\\|,)/', '|', $results );
$results = preg_replace( '/(^,)|(,$)/', '', $results );
$results = str_replace( ',,', ',', $results );
Is there an easier or more concise way to do this?