Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

How to convert a string to title case in PHP

3 Answers

0 votes
echo ucwords(strtolower("string programming functions")) . "\n";

echo ucwords(strtolower("STRING PROGRAMMING FUNCTIONS")) . "\n";



/*
 
run:
 
String Programming Functions
String Programming Functions
 
*/


answered Aug 29, 2014 by avibootz
edited May 8, 2024 by avibootz
0 votes
function ToTitleCase($string) { 
    $len = strlen($string); 
    $i = 0; 
    $last = ""; 
    $title = "";
       
    $string = strtoupper($string); 
    while ($i < $len) {
        $char = substr($string, $i, 1); 
        if (preg_match("/[A-Z]/", $last))
            $title .= strtolower($char); 
        else
            $title .= strtoupper($char); 
        $last = $char; 
        $i++; 
    }
     
    return($title); 
} 
  
  
echo ToTitleCase("string programming functions") . "\n";
 
echo ToTitleCase("STRING PROGRAMMING FUNCTIONS") . "\n";
 
 
 
/*
  
run:
  
String Programming Functions
String Programming Functions
  
*/

 



answered May 8, 2024 by avibootz
0 votes
function ToTitleCase($string) { 
    $string = strtolower($string);
    $words = explode(" ", $string); 
    
    $titleCaseWords = array_map('ucfirst', $words); 
  
    $titleCaseString = implode(" ", $titleCaseWords); 
    
    return $titleCaseString;
} 
 
 
echo ToTitleCase("string programming functions") . "\n";

echo ToTitleCase("STRING PROGRAMMING FUNCTIONS") . "\n";



/*
 
run:
 
String Programming Functions
String Programming Functions
 
*/

 



answered May 8, 2024 by avibootz

Related questions

1 answer 138 views
1 answer 82 views
1 answer 95 views
1 answer 92 views
1 answer 93 views
1 answer 130 views
1 answer 89 views
...