1

Possible Duplicate:
How do I display the first letter as uppercase?
PHP capitalize first letter of first word in a sentence

I want to uppercase the first letter in a sentence and after a period. Can anyone suggest how to do?

For example,

//I have the following in a language class.
"%s needs to identify areas of strength and 
weakness. %s sets goals for self-improvement."; 

// in a view
$contone=$this->lang->line($colstr);// e.g get the above string.
//$conttwo=substr($contone, 3);//skip "%s " but this doesnot work when there 
//are more than one %s
$conttwo=str_replace("%s ", "", $contone);// replace %s to none 
$contthree = ucfirst($conttwo); // this only uppercase the first one

I want the following output.

Needs to identify areas of strength and 
weakness. Sets goals for self-improvement.
halfer
  • 19,824
  • 17
  • 99
  • 186
shin
  • 31,901
  • 69
  • 184
  • 271
  • 1
    Reliably? Might not be possible. It would be hard to differentiate between a full stop and a decimal point. You could explode() the string on the . character, ucfirst each element in the array and implode() the result, or preg_replace for . characters followed by at least one space an an [a-z], but there's plenty of scope for error in either approach. – GordonM Jan 21 '12 at 07:36

2 Answers2

2

Try below.

It will run the function to capitalize every letter AFTER a full-stop (period) in a string having multiple sentences.

    $string = ucfirst(strtolower($string));     

    $string = preg_replace_callback('/[.!?].*?\w/', create_function('$matches', 'return strtoupper($matches[0]);'),$string);

    echo $string;

Please do required changes.

  • There is a problem with your solution. If the sentence has proper names cannot be converted to lowercase, then the correction follows: $string = ucfirst(preg_replace_callback('/,\s[A-Z]/', create_function('$matches', 'return strtolower($matches[0]);'), $string)); $string = preg_replace_callback('/[.]\s([a-z])/', create_function('$matches', 'return strtoupper($matches[0]);'),$string); – Karra Max Dec 28 '21 at 10:01
0

Try this:

<?php
//define string
$string = "your sentences";

//first we make everything lowercase, and then make the first letter if the entire string capitalized
$string = ucfirst(strtolower($string));

//now we run the function to capitalize every letter AFTER a full-stop (period).
$string = preg_replace_callback('/[.!?].*?\w/', create_function('$matches', 'return strtoupper($matches[0]);'),$string);

//print the result
echo $string;

?>
Kichu
  • 3,284
  • 15
  • 69
  • 135