PHP: Strip number from string

February 11, 2012

I recently ran across a situation where a string being used as a key contained numeric characters. The requirements specified stated that the new data structure keys do not contain numbers. This is a very simple problem to solve and can be done with a simple PHP str_replace. The following function is what I came up with.

function trim_numbers($str) {
    $n = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    return str_replace($n, '', $str);
}