-
Editing sentences
Hello guys, I need a complex editing code. I am displaying user data present in table on a html page. I need a code that does all of the following.
1. Ensure that first word of a sentence is capital. The sentence may end with . ? or !
2. Alphabet I should be always capital and never i.
3. Unless specified, a sentence must end with a period.
4. There should be a single white space after period, comma or question mark.
I need one efficient code to do all this. Can anyone do it for me?
-
Re: Editing sentences
This sounds an awful lot like a homework assignment...
Why don't you try to code it, and someone can help you out when you run into issues?
-
Re: Editing sentences
Read up on Regular Expressions here if you don't already know how to use/write them: http://www.regular-expressions.info/
After you become familiar with the basics of RegEx you can implement a RegEx replace using preg_replace: http://php.net/manual/en/function.preg-replace.php
If you use preg_replace_callback you can use ucfirst in the callback to uppercase the first letter of a string: http://php.net/manual/en/function.ucfirst.php
The general idea is that you use regular expressions to isolate each sentence. You then use ucfirst on each sentence to make sure the first letter of the sentence is capitalized. Your regex can also isolate occurances of the letter 'i' so that you may capitalize it. You could also use a str_replace for the letter 'i'.
str_replace and preg_replace can both deal with that extra white space after the sentence.