How to make the first char string string in PHP?

I cannot use strtolower since it affects all char. Should I use some sort of regex?

I get a string that is a product code, I want to use this product code as a search key in the other palm with the first letter made in lower case.

+3
source share
4 answers

Just do:

$str = "STACKoverflow";
$str[0] = strtolower($str[0]); // prints sTACKoverflow

and if you use >=5.3, you can do:

$str = lcfirst($str);
+9
source

Try

  • lcfirst - Make a line of the first character of a lowercase letter

and for PHP <5.3 add this to 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;
    }
}

strolower ing , PHP- PHP5.3

. , . .

+16

use icfirst ()

<?php
$foo = 'HelloWorld';
$foo = lcfirst($foo);             // helloWorld

$bar = 'HELLO WORLD!';
$bar = lcfirst($bar);             // hELLO WORLD!
$bar = lcfirst(strtoupper($bar)); // hELLO WORLD!
?>
+1
source

For the multibyte first line of a line, none of the above examples will work. In this case you should use:

function mb_lcfirst($string)
{
    return mb_strtolower(mb_substr($string,0,1)) . mb_substr($string,1);
}
0
source

Source: https://habr.com/ru/post/1744755/


All Articles