问题描述
我的 TXT 文件格式如下:
123451234512345
123451234512345
12345-12345-12345
12345-12345-12345
这是我尝试过的:
$codes = "codes.txt";
$unformatedCode = file($codes,FILE_IGnorE_NEW_LInes);
$abcd = "-";
foreach ($array as $value) {
$text = (string)$unformatedCode;
$arr = str_split($text,"");
$formatedCode = implode("-",$arr);
echo $formatedCode;
}
解决方法
试试这个,
int main(int argc,char **argv) {
CallableI myCallable;
Point3D<double> p1(14.3,12.3,2.0,myCallable);
return 0;
结果:
<?php
$arr = array_map(
fn($v) => implode('-',str_split($v,5)),file("codes.txt",FILE_IGNORE_NEW_LINES)
);
print_r($arr);
如果您想回显结果,请执行 Array
(
[0] => 12345-12345-12345
[1] => 12345-12345-12345
)
可能如果可以写得更短
$codes = "codes.txt";
$array = file($codes,FILE_IGNORE_NEW_LINES);
$p = "/([1-9]{5})([1-9]{5})([1-9]{5})/i";
$r = "${1}-${2}-${3}";
foreach ($array as $a) {
echo preg_replace($p,$r,$a);
}