在PHP中,如何拆分这些字符串,例如:
OBKFA4F26ORJI3ALFYI3QB4ZN
成:
OBKFA-4F26O-RJI3A-LFYI3-QB4ZN
答案 0 :(得分:2)
使用此正则表达式,然后将匹配替换为-
.{5}(?!$)\K
OR
$re = "/.{5}(?!$)/m";
$str = "OBKFA4F26ORJI3ALFYI3QB4ZN";
$subst = "$0-";
$result = preg_replace($re, $subst, $str);
echo $result;
答案 1 :(得分:0)
试试这个:
<?php
$string = "OBKFA4F26ORJI3ALFYI3QB4ZN";
$output = str_split($string, 5);
echo implode("-",$output);
?>
输出:
OBKFA-4F26O-RJI3A-LFYI3-QB4ZN