我有这个代码:
$CapDeliveryCityANDState = str_replace('\, ', '\,', ucwords(str_replace('\,', '\, ', strtolower($CapDeliveryCityANDState))));
$CapDeliveryCityANDState = strrev(ucfirst(strrev($CapDeliveryCityANDState)));
这使得每个单词的第一个字母大写,其余的小写,在逗号“,”之后,它使前两个字母大写,但我想添加另一件事,如果有一个“'”,则将字母大写,所以,到目前为止,我对这个例子的效果很好:
cHiCago, il
会成为Chicago, IL
但如果是的o'fallon, mo
话,O'fallon, MO
但我希望它是O'Fallon, MO
(撇号后的大写字母)
谢谢您的帮助...
解决方案是:
$CapDeliveryCityANDState = str_replace('\, ', '\,', ucwords(str_replace('\,', '\, ', strtolower($CapDeliveryCityANDState))));
$CapDeliveryCityANDState = strrev(ucfirst(strrev($CapDeliveryCityANDState)));
if(strpos($CapDeliveryCityANDState, "'")) {
$pos = strpos($CapDeliveryCityANDState, "'") + 1;
}
$CapDeliveryCityANDState = substr_replace($CapDeliveryCityANDState, strtoupper($CapDeliveryCityANDState[$pos]), $pos, 1);
$CapDeliveryCityANDState[$l=strlen($CapDeliveryCityANDState)-2] = strtoupper($CapDeliveryCityANDState[$l]);
如果他们需要,可以为任何人添加更多代码:
$CapDeliveryCityANDState = $contact_CityandStateSTR;
if(strlen($CapDeliveryCityANDState) >= 5){ //If more then 5 letters then do the below
$CapDeliveryCityANDState = str_replace('\, ', '\,', ucwords(str_replace('\,', '\, ', strtolower($CapDeliveryCityANDState))));
$CapDeliveryCityANDState = strrev(ucfirst(strrev($CapDeliveryCityANDState)));
if(strpos($CapDeliveryCityANDState, "'")) {
$pos = strpos($CapDeliveryCityANDState, "'") + 1;
$CapDeliveryCityANDState = substr_replace($CapDeliveryCityANDState, strtoupper($CapDeliveryCityANDState[$pos]), $pos, 1);
}
$mystringz = $CapDeliveryCityANDState;
$findmez = ',';
$posz = strpos($mystringz, $findmez);
if ($posz !== false) {
// IF NOT FALSE THEN CAP. LAST 2 LETTERS (STATE)
$CapDeliveryCityANDState[$l=strlen($CapDeliveryCityANDState)-2] = strtoupper($CapDeliveryCityANDState[$l]);
} else {
// ELSE IF FALSE THEN LEAVE AS IS
$CapDeliveryCityANDState = $contact_CityandStateSTR;
}
}
$CapDeliveryCityANDState = str_replace(" ,", ",", $CapDeliveryCityANDState); //remove space after city
$CapDeliveryCityANDState = str_replace(",", ", ", $CapDeliveryCityANDState); //add space after comma
$CapDeliveryCityANDState = preg_replace('!\s+!', ' ', $CapDeliveryCityANDState); //Check and remove double space