如何用分隔符分割或拆分 php 中的字符串?

How to devide or split strings in php with separator?

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

OBKFA4F26ORJI3ALFYI3QB4ZN

进入:

OBKFA-4F26O-RJI3A-LFYI3-QB4ZN

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

.{5}(?!$)\K

DEMO

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

DEMO

试试这个:

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

输出:

OBKFA-4F26O-RJI3A-LFYI3-QB4ZN

ONLINE DEMO