How to replace a space with an underscore in PHP?

Use str_replace function of php to replace a space with an underscore. I am posting this for those of you who have struggled with this, in hopes that it just might save a couple of hours.
It is such a simple solution.
$string = "this is example";
$final = str_replace(" ","_",$string);
//output : this_is_example
Alternative solution can be to use regular expression.

Comments

Popular Posts