convert string spaces to underscore in php code example Example: php convert spaces to underscores $str = str_replace(' ', '_', $str);