如何在分隔符中划分或拆分php中的字符串?

时间:2015-01-21 09:26:07

标签: php string split

在PHP中,如何拆分这些字符串,例如:

OBKFA4F26ORJI3ALFYI3QB4ZN

成:

OBKFA-4F26O-RJI3A-LFYI3-QB4ZN

2 个答案:

答案 0 :(得分:2)

使用此正则表达式,然后将匹配替换为-

.{5}(?!$)\K

DEMO

OR

$re = "/.{5}(?!$)/m";
$str = "OBKFA4F26ORJI3ALFYI3QB4ZN";
$subst = "$0-";
$result = preg_replace($re, $subst, $str);
echo $result;

DEMO

答案 1 :(得分:0)

试试这个:

<?php
$string = "OBKFA4F26ORJI3ALFYI3QB4ZN";
    $output = str_split($string, 5);
    echo implode("-",$output);
?>

输出:

  

OBKFA-4F26O-RJI3A-LFYI3-QB4ZN

ONLINE DEMO