我很想看到一双新鲜的眼睛看着我的问题,这让我很生气。任何帮助将不胜感激。
只需2行PHP代码,如果用户在电话号码的开头输入,则尝试剥离第一个'44':
$telephone = '44789562356';
$telephone = str_replace(' ','',$telephone);
$telephone = str_replace('+44','0',$telephone);
if(strpos($telephone,"44")==0){
$telephone = substr($telephone,2);
$telephone = '0'.$telephone;
}
为什么从所有电话号码中删除“7”?
答案 0 :(得分:0)
就像Colin评论的那样,您需要对===
的返回使用严格的比较strpos()
,因为如果找不到子字符串则返回false
,如果0
则返回false == 0
它位于字符串的开头,false === 0
为真,if( preg_match('/^44/', $telephone) ) { ... }
为假。
或者,您可以使用正则表达式仅在字符串的开头指定匹配,如下所示:
preg_replace('/^44/', '0', $telephone);
或者用它替换:
$telephone = '+44-789 56-2356 ask for larry';
$telephone = preg_replace('/[^0-9]/','',$telephone); // remove all non-numeric characters
$telephone = preg_replace('/^44/','0',$telephone);
echo $telephone;
// output: 0789562356
您的代码可以简化为以下内容:
{{1}}