php - 格式化txt文件
问题描述
我有一个 TXT 文件格式,如下所示:
123451234512345
123451234512345
我想用这种格式的php格式化文件:。
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;
}
解决方案
尝试这个,
<?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
)
如果要回显结果,请执行<?= implode(PHP_EOL, $arr) ?>
推荐阅读
- time-complexity - 时间复杂度会根据语言而变化吗?
- arduino - arduino yun 和 rfid shield 兼容性
- ios - 在屏幕右下角放置一个按钮
- python - 使用 Selenium python 找不到输入框
- r - 在ftable中添加列和行总计
- python-3.x - 寻找循环的优化
- sql - 通过在 SQL 中按日期范围聚合来计算流失率
- postgresql - pod 应该用来连接到同一集群中的 postgresql pod 的连接字符串?
- javascript - 我使用 v-model 作为输入和名称。我试图了解访问其数据的不同方式
- css - css fontawesome 图标与按钮 ReactJS 内的两个文本行对齐