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