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?
Use the replace() method to replace spaces with dashes in a string, e.g. str. replace(/\s+/g, '-') . The replace method will return a new string, where each space is replaced by a dash.
The strtolower() function converts a string to lowercase. Note: This function is binary-safe. Related functions: strtoupper() - converts a string to uppercase.
The ucfirst() function converts the first character of a string to uppercase. Related functions: lcfirst() - converts the first character of a string to lowercase. ucwords() - converts the first character of each word in a string to uppercase.
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))); ?>
For a string wrap you can use the dedicated wordwrap function.
str_replace
str_replace online documentation
<?php $str = 'Convert spaces to dash and LowerCase with PHP'; echo str_replace(' ', '-', strtolower($str)); // return: convert-spaces-to-dash-and-lowercase-with-php
wordwrap
wordwrap online documentation
$str = 'Convert spaces to dash and LowerCase with PHP'; echo wordwrap(strtolower($str), 1, '-', 0); // return: convert-spaces-to-dash-and-lowercase-with-php
online code: https://3v4l.org/keWGr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With