Search code examples
phparraysstringstrip

How to strip from a string those which are not in the a-z and 0-9 range?


I know a version of this but i'm looking for the simplest way?

$string = "HeLLo$ my222 name is zolee0802343134";
$string = strtolower($string);
$replacement  = range (a, z);
$replacement2 = range (0, 9);

//
What comes here?
// 

I want to get this ->
$string = "hello my name is zolee";

Solution

  • Use regex for simplicity.

    $string = "HeLLo$ my222 name is zolee0802343134";
    echo preg_replace("/[^a-z ]/i", "", $string);
    

    http://codepad.org/eDmXrnYR