How to capitalize first letter of first word in a sentence?

前端 未结 7 849
猫巷女王i
猫巷女王i 2020-11-27 20:43

I am trying to write a function to clean up user input.

I am not trying to make it perfect. I would rather have a few names and acronyms in lowercase than a full par

7条回答
  •  遥遥无期
    2020-11-27 20:58

    How about this? Without Regex.

    $letters = array(
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
     'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
    );
    foreach ($letters as $letter) {
        $string = str_replace('. ' . $letter, '. ' . ucwords($letter), $string);
        $string = str_replace('? ' . $letter, '? ' . ucwords($letter), $string);
        $string = str_replace('! ' . $letter, '! ' . ucwords($letter), $string);
    }
    

    Worked fine for me.

提交回复
热议问题