2017-07-30 463 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; 
} 
相关问题