假设我必须关注php脚本:
$str = "be43f751d380a70d13287006a5f7795a";
$arr = str_split($str, 2);
$strT = implode("", substr_replace($arr ,"",-1));
printf('%s:%s' . PHP_EOL, $str, $strT);
输出:be43f751d380a70d13287006a5f7795a:b4f5d8a01270af75
这适用于单个字符串,但如果我想将其应用于多个字符串,具有多个字符串结尾的文件,该怎么办:
cat strings.txt
dc7380f92396b65be0c9dc2fda593acc
e5a391e5b84ebcd51dc467de52f88558
4ec7814976d12b40638fbe311856f84f
期望的输出:
dc7380f92396b65be0c9dc2fda593acc:d78f29b5ecd2d53c
e5a391e5b84ebcd51dc467de52f88558:ea9eb4bd1c6d5f85
4ec7814976d12b40638fbe311856f84f:4c847d2468b315f4
如果我们可以自己在命令行中指定文件,那就太棒了:
php script.php strings.txt > trimmed.txt
谢谢!
答案 0 :(得分:1)
好吧,我也建议阅读php手册,但不是foreach,而是file()函数。这可以这样做:
<?php
foreach(file($argv[0]) as $line)
{
$arr = str_split($line, 2);
$strT = implode("", substr_replace($arr ,"",-1));
printf('%s:%s' . PHP_EOL, $str, $strT);
}
答案 1 :(得分:0)
对于命令行解决方案,请运行以下命令:
while read str; do php -f myfun.php $str; done < strings.txt
其中myfun.php是以下PHP脚本:
$arr = str_split($argv[1], 2);
$strT = implode("", substr_replace($arr ,"",-1));
printf('%s:%s' . PHP_EOL, $argv[1], $strT);