PHP function library 05: PHP implements correct uppercase letters for English titles

Source: Internet
Author: User

 

<?
// Enable the title to be correctly capitalized

// Except for a, an. the, bu, as if, and, or, nor, of, by, the first letter of all other words is capitalized.

Function title_upcase ($ str ){
// Uppercase

$ Str = ucwords ($ str );
// Returns an array containing all the words in the string and indexed by the position of the word in the string.

$ Wordlist = str_word_count ($ str, 2 );
// Exclude the first and last elements in the array, because it does not need to be changed to lowercase.

$ Wordlist = array_slice ($ wordlist, 1,-1, true );
// If the following words are contained, all lowercase letters are used.

Foreach ($ wordlist as $ position => $ word ){
Switch ($ word ){
Case 'A ':
Case 'A ':
Case ''the ':
Case 'but ':
Case 'as ':
Case 'if ':
Case 'and ':
Case 'or ':
Case 'nor ':
Case 'of ':
Case 'by ':
$ Lower = strtolower ($ word );
$ Str {$ position }=$ lower {0 };
}
}
Return $ str;
}
?>

 

Function Description and example

 

<?
$ Sample = "a study of interesteller galaxies as presented by scientist ";
$ Upcased = title_upcase ($ sample );
Echo $ sample; // a study of interesteller galaxies as presented by scientist

Echo $ upcased; // A Study of Interesteller Galaxies as Presented by Scientist
?>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.