2017-07-30 465 views
0

我從具有phone_number字段的表中提取記錄。在表格中,一些電話號碼的順序不正確。正確的順序應該是233,然後是用戶的電話號碼,但有些記錄以用戶的電話號碼開頭。如果第一個字符爲0,則替換字符串的第一個字符

即:不是233243000(233xxxxxx所以正確的順序)一些電話號碼,如:0243000(即不正確的順序)

什麼我想要做的是,如果只數上以零開始,就應及時更換與233,以便所有的數字成爲正確的順序。

+0

使用PHP函數SUBSTR – Akintunde007

回答

0

測試第一個字符,如果它爲零,則將其替換。

if (substr($pn, 0, 1) === '0') { 
    $pn = '233' . substr($pn, 1); 
} 

你也可以使用其他標準,如原始值的長度等,以確保您的結果是你所期望的,而不是一個特殊的價值。例如,如果原始值爲023333623236,則執行上述轉換可能不是您想要的。

+0

是的,這是真的 – user6579134

+0

u能幫助我與檢查長度過的腳本 – user6579134

0
$number = '043234'; 
$const = '233'; 
if(substr($number,0) === '0'){ 
echo $number = $const.''.substr(1, strlen($number)); 
} 
else{ 
echo $number; 
} 
0
$phoneNumber = '023343234'; 
$countryCode = '233'; 

if(substr($phoneNumber, 0, 4) == '0233'){ 
    // if the number has 233 but it started with 0 
    echo $phoneNumber = $countryCode.''.substr($phoneNumber, 4); 

}elseif(substr($phoneNumber, 0, 1) == '0'){ 
    // if the number started with 0 
echo $phoneNumber = $countryCode.''.substr($phoneNumber, 1); 
}else{ 
    // if the number is in correct format 
echo $phoneNumber; 
} 
相關問題