tgavin Posted March 26, 2008 Share Posted March 26, 2008 I'm using these functions to capitalize words, but in a clever way. I didn't write this, but am hoping somebody could help me make a small modification. If one of these words begins a sentence, or is after a double quote at the beginning of a sentence, the word still goes lowercase. Example Entering To The Market would return to the Market. Should be To the Market Entering "To The Market" would return "to the Market". Should be "To the Market" <?php function lower_articles($str) { return preg_replace("/(?<=(?<!:|’s)\W)(A|An|And|At|For|In|Of|On|Or|The|To|With|Vs)(?=\W)/e",'strtolower("$1")',$str); } function ucsmart($str) { $str = preg_replace('/([^a-z\']|^)([a-z])/e', '"$1".strtoupper("$2")', strtolower($str)); return lower_articles($str); } ?> Link to comment https://forums.phpfreaks.com/topic/97917-change-to-lowercase-function/ Share on other sites More sharing options...
Jeremysr Posted March 26, 2008 Share Posted March 26, 2008 Try this: <?php function lower_articles($str) { return preg_replace("/(?<=(?<!:|’s)\W)(A|An|And|At|For|In|Of|On|Or|The|To|With|Vs)(?=\W)/e",'strtolower("$1")',$str); } function ucsmart($str) { $str = preg_replace('/([^a-z\']|^)([a-z])/e', '"$1".strtoupper("$2")', strtolower($str)); if ($str[0] == '"' || $str[0] == "'") { $str[1] = strtoupper($str[1]); } else { $str[0] = strtoupper($str[0]); } return lower_articles($str); } ?> This assumes that the string either begins with a letter (or number I guess) or begins with a double- or single-quote and then a letter. Link to comment https://forums.phpfreaks.com/topic/97917-change-to-lowercase-function/#findComment-500969 Share on other sites More sharing options...
tgavin Posted March 26, 2008 Author Share Posted March 26, 2008 thank you. still making the first letter after a " lowercase. Perhaps I'm running this through the functions incorrectly? $title = ucsmart(strip_tags(stripslashes($_POST['title'][$i]))); Link to comment https://forums.phpfreaks.com/topic/97917-change-to-lowercase-function/#findComment-501673 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.