How to Convert String to Sentence Case in PHP

This post is all about How to Convert String to Sentence Case in PHP. Why Sentence case is required? Whenever you want to know your text content case is properly formatted or not, it either lower case, upper case or, camelcase. Same like sentence case is a custom function. Some cases in website whenever you take input from user randomly we have to make formatted as a sentence case, because the user may type input in unformatted, but when it will show in the website it should be properly formatted. In PHP we have some default functions to format text like ucfirst() , lcfirst(), strtoupper(),strtolower() but sentence case is not available, so using these methods we can make custom function called as sentenceCase();



Convert text to sentence case in php

What is sentceCase() function?
It is a function which takes a parameter as text or string makes the first letter as Capital and put a space for consecutive sentence in a paragraph.

Using PHP, you can easily convert any string to sentence case. In this post, we are going to show you a simple PHP function for convert string to sentence case.



Here we are using three default function of PHP. strlen(), substr() and strtoupper()
First, we have to check a letterwise by using for loop then finding the substring within the string like “dot” or “!” or “?” , this string tells us this is the end of the sentence. So the next letter of the consequence word should be capital and some space between also.

 

Tags: ,

Advertisements