Convert spaces to dash and lowercase with PHP

caustic picture caustic · Mar 11, 2015 · Viewed 38.2k times · Source

I've tried a few long methods but I think I'm doing something wrong.

Here is my code

<?php print strtolower($blob); ?>

Which makes $blob lowercase, but additionally I need any spaces in $blob to be removed and replaced by a dash (-).

I tried this, but it didn't work

<?php print (str_replace(' ', '-', $string)strtolower($blob)); ?>

Can I accomplish this all in the one line?

Answer

Alexander O&#39;Mara picture Alexander O'Mara · Mar 11, 2015

Yes, simply pass the return value of strtolower($blob) as the third argument of str_replace (where you have $string).

<?php print (str_replace(' ', '-', strtolower($blob))); ?>