Replace spaces with a dash in a URL

Robdogga55 picture Robdogga55 · Jan 30, 2013 · Viewed 57.6k times · Source

I managed to replace special characters such as : ; / etc in my URL but now it has the spaces again. Here is my code:

<h3><a href="<?php echo (isset($row_getDisplay['post_id']) ? $row_getDisplay['post_id'] : ''); ?>_<?php echo str_replace(array(':', '\\', '/', '*'), ' ', urldecode($row_getDisplay['title'])); ?>.html" ><?php echo (isset($row_getDisplay['title']) ? $row_getDisplay['title'] : ''); ?></a></h3>

I want it to like it is remove special characters as well as replace spaces with dashes.

Answer

ka_lin picture ka_lin · Jan 30, 2013

Try str_replace(' ', '-', $string);