1<?php
2 $string = "hello php";
3 $replace = str_replace(" ", "_", $string);
4 echo $replace; // hello_php
5?>
1// Clean up multiple dashes or whitespaces
2$string = preg_replace("/[\s-]+/", " ", $string);
3// Convert whitespaces and underscore to dash
4$string = preg_replace("/[\s_]/", "-", $string);