PHP str_replace replace spaces with underscores by Here is top answer to str_replace replace spaces with underscores <?php $journalName = preg_replace('/\s+/', '_', $journalName); ?>