I think, therefore I am. I am, therefore I sail

PHP: Strip number from string

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);
}

Previous

San Dimas High!

Next

Crystal Cove, my new favorite beach

1 Comment

  1. Sam

    There is a simpler way: trim($str, ‘0..9’);

Powered by WordPress & Beards