Make Sentence Case In PHP

// Run text through this to make it sentence case…
// “the pretty pink sandwich” becomes:
// “The pretty pink sandwich”

function sentence_case($s) {
   $str = strtolower($s);
   $cap = true;
   for($x = 0; $x < strlen($str); $x++){
       $letter = substr($str, $x, 1);
       if($letter == "." || $letter == "!" || $letter == "?"){
           $cap = true;
       }elseif($letter != " " && $cap == true){
           $letter = strtoupper($letter);
           $cap = false;
       }
       $ret .= $letter;
   }
   return $ret;
}
to php by tracyfloyd on Thu Sep 28 22:15:41 +0000 2006

Comments on this post

pingu7931 posts on Nov 23, 2011 at 22:34
Found this as a first hit on Google and even though it’s an old post I thought I’d add an amendment. This will preserve existing proper noun capitals – e.g. “I went to the Bay of Bengal on Tuesday.” Old code would give “I went to the bay of bengal on tuesday.” I renamed the parameter to $str and removed converting the entire text to lowercase.

function sentence_case($str) {
   // $str = strtolower($s);
   $cap = true;
   $ret='';
   for($x = 0; $x < strlen($str); $x++){
	   $letter = substr($str, $x, 1);
	   if($letter == "." || $letter == "!" || $letter == "?"){
		   $cap = true;
	   }elseif($letter != " " && $cap == true){
		   $letter = strtoupper($letter);
		   $cap = false;
	   } else {
		   // If letter was already uppercase then leave it alone
		   if(strtoupper($letter) != $letter) $letter = strtolower($letter);
	   }

	   $ret .= $letter;
   }
   return $ret;
}

pingu7931 posts on Nov 23, 2011 at 22:37
Lol – just realised last else is a tautology – can be removed:

function sentence_case($str) {
   $cap = true;
   $ret='';
   for($x = 0; $x < strlen($str); $x++){
	   $letter = substr($str, $x, 1);
	   if($letter == "." || $letter == "!" || $letter == "?"){
		   $cap = true;
	   }elseif($letter != " " && $cap == true){
		   $letter = strtoupper($letter);
		   $cap = false;
	   } 
	   $ret .= $letter;
   }
   return $ret;
}

pingu7931 posts on Nov 23, 2011 at 22:44
Slight tweek. If we have a string that is all in capitals then we can lowercase it first. Sorry about lots of posts but I’m trying to clean up some old posts in a blogging database. So I am using on real live data that people have posted. And finding issues.

function sentence_case($str) {
   $cap = true;
   $ret='';
   $allcaps = true;
   for($x = 0; $x < strlen($str); $x++){
	   $letter = substr($str, $x, 1);
	   if($letter != strtoupper($letter)){
		   $allcaps = false;
		   break;
	   }
   }
   if($allcaps) $str = strtolower($str);
   for($x = 0; $x < strlen($str); $x++){
	   $letter = substr($str, $x, 1);
	   if($letter == "." || $letter == "!" || $letter == "?"){
		   $cap = true;
	   }elseif($letter != " " && $cap == true){
		   $letter = strtoupper($letter);
		   $cap = false;
	   } 
	   $ret .= $letter;
   }
   return $ret;
}

You need to create an account or log in to post comments to this site.

Advertisements
By Rz Rasel Posted in Php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s