Search code examples
phpstringreplaceconverters

Convert spaces to dash and lowercase with PHP


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?


Solution

  • 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))); ?>