城市,州的正确案例标题,包括撇号

时间:2012-12-04 15:33:36

标签: php

我有这段代码:

$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

1 个答案:

答案 0 :(得分:1)

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