• 技术文章 >php教程 >php手册

    php字符串首字母转换大小写

    2016-06-06 16:05:05原创623

    将字符串中  每个单词的首字母转换为大写:ucwords()

    $foo = 'hello world!';
    $foo = ucwords($foo);             // Hello World!

    $bar = 'HELLO WORLD!';
    $bar = ucwords($bar);             // HELLO WORLD!
    $bar = ucwords(strtolower($bar)); // Hello World!
    ?>

    第一个词首字母变大写:ucfirst()

    $foo = 'hello world!';
    $foo = ucfirst($foo);             // Hello world!

    $bar = 'HELLO WORLD!';
    $bar = ucfirst($bar);             // HELLO WORLD!
    $bar = ucfirst(strtolower($bar)); // Hello world!
    ?>

    第一个词首字母小写lcfirst()


    $foo = 'HelloWorld';
    $foo = lcfirst($foo);             // helloWorld

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

    字母变大写:strtoupper()

    字母变小写:strtolower()