我从这样的表格发送了电话号码:
0641234567
064-123/4567
064/123-4567
3816412345678
它必须像这样:
+3816412345678
带+,不带0,最多14个字符,包括“ +”。
我如何使用正则表达式解决这个问题?
答案 0 :(得分:0)
您显示的数据可能不完整,并且可能存在一些我们无法正确看到的情况。根据我们目前所看到的,可以通过用064
替换前导38164
,然后去除所有破折号和正斜杠来实现替换。使用preg_replace
:
$input = "064-123/4567";
$output = preg_replace('/[\/-]/', '', preg_replace('/^064/', '38164', $input));
echo $input . "\n" . $output;
064-123/4567
381641234567
答案 1 :(得分:0)
您需要的是基本上用三个规则进行多次替换
-
或/
+381
开头的零代替+
选中此PHP Demo,
$arr = ['0641234567','064-123/4567','064/123-4567','3816412345678'];
foreach($arr as $s) {
echo $s." --> ".preg_replace(['/^0/', '/^(?=[1-9])/', '/[-\/]/'], ['+381', '+', ''], $s)."\n";
}
打印
0641234567 --> +381641234567
064-123/4567 --> +381641234567
064/123-4567 --> +381641234567
3816412345678 --> +3816412345678
让我知道您的任何案件是否被发现。