find spaces in string and replace with underscore php code example Example: php replace space with underscore $str = str_replace(' ', '_', $str);