Jump to content

Change to lowercase function


tgavin

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.