I cannot use strtolower as it affects all characters. Should I use some sort of regular expression?
I'm getting a string which is a product code. I want to use this product code as a search key in a different place with the first letter made lowercase.
Try
lcfirst
— Make a string's first character lowercaseand for PHP < 5.3 add this into the global scope:
if (!function_exists('lcfirst')) {
function lcfirst($str)
{
$str = is_string($str) ? $str : '';
if(mb_strlen($str) > 0) {
$str[0] = mb_strtolower($str[0]);
}
return $str;
}
}
The advantage of the above over just strolower
ing where needed is that your PHP code will simply switch to the native function once you upgrade to PHP 5.3.
The function checks whether there actually is a first character in the string and that it is an alphabetic character in the current locale. It is also multibyte aware.
Just do:
$str = "STACK overflow";
$str[0] = strtolower($str[0]); // prints sTACK overflow
And if you are using 5.3 or later, you can do:
$str = lcfirst($str);
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