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

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


$output = preg_replace('/([.!?])\s*(\w)/e', "strtoupper('\\1 \\2')", ucfirst(strtolower($input)));

Since the modifier e is deprecated in PHP 5.5.0:

$output = preg_replace_callback('/([.!?])\s*(\w)/', function ($matches) {    return strtoupper($matches[1] . ' ' . $matches[2]);}, ucfirst(strtolower($input)));


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 resultecho $str . "\n";?>

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


This:

<?$text = "abc. def! ghi? jkl.\n";print $text;$text = preg_replace("/([.!?]\s*\w)/e", "strtoupper('$1')", $text);print $text;?>Output:abc. def! ghi? jkl.abc. Def! Ghi? Jkl.

Note that you do not have to escape .!? inside [].