Maybe the variable $input has still the value [[~22]] when the snippet ContentID is called.
You could test if this works:
<?php
$search_pattern="/\[\[~(\d+)\]\]/";
if (preg_match($search_pattern, $input, $match)){
return $match[1];
} else {
return $input;
}