0

我有这个代码:

$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
4

1 回答 1

1

使用 strstr() 查找撇号。如果存在,将值分解为两部分(撇号之前和之后),将它们大写,然后再次将它们 implode() 放在撇号上。

于 2012-12-04T15:40:25.610 回答