读入文件,将[linebreak char]替换为??。并创建新的字符串/文件
尝试了strtr和str_replace,但是只能替换已知字符,不能随机替换。
我已经尝试过:
$file = file_get_contents('./tel6.out', true);
$s=strtr($file, array( '' => '??' ));
源文件格式:
答案 0 :(得分:0)
$st= "0090C9
00AV0X7Y79T543C
00AV0X6Y103T245C
00AV0X60Y103T248C
00AV0X101Y103T24B8
00AV0X131Y103T24BB
00AV0X10Y111T0484
00AV0X63Y111T048C
00AV0X133Y111T04BA
00AV0X151Y111T04BA
00AV0X166Y56T1499
00AV0X167Y61T1496
00AV0X17Y124T2491
00AV0X11Y129T2490
00AV0X14Y125T248F
00AV0X14Y130T248B
00AV0X17Y129T2496
00AV0X10Y134T248B
00AV0X34Y134T2491
00AV0X10Y149T2491
00AV0X10Y152T248B";
$ret = "";
//split the string by newline
$splitted = trim(explode("\n", $st));
foreach ($splitted as $s2)
{
//overwrite a random char in this chunk of the string with the first '?'
$s2[rand(0, count($s2))] = '?';
//overwrite a random char in this chunk of the string with the second '?'
$s2[rand(0, count($s2))] = '?'; //this '?' may overwrite the first one
//concatenate a string with this chunk plus '??' (that stand now instead of newline)
$ret = $ret . $s2 . "??";
}
//show the resulting string
var_dump($ret);
注意:可能会发生两个随机的'?'被放在同一位置放置一串字符串,因此仅产生一个“?”在一大串的字符串中。 (当然可以避免)。
如果您覆盖了字符,则无需知道要替换的字符。