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

前端 未结 7 830
猫巷女王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 21:15

    Here is the code that does as you wanted:

    <?php
    
    $str = "paste your code! below. codepad will run it. are you sure?ok";
    
    //first we make everything lowercase, and 
    //then make the first letter if the entire string capitalized
    $str = ucfirst(strtolower($str));
    
    //now capitalize every letter after a . ? and ! followed by space
    $str = preg_replace_callback('/[.!?] .*?\w/', 
      create_function('$matches', 'return strtoupper($matches[0]);'), $str);
    
    //print the result
    echo $str . "\n";
    ?>
    

    OUTPUT: Paste your code! Below. Codepad will run it. Are you sure?ok

    0 讨论(0)
提交回复
热议问题