duande1146 2019-05-16 09:47
浏览 125

替换(换行)加两个随机字符?? 在字符串中

Read in file, replace [linebreak char] with ?? and create new string/file

Tried both strtr and str_replace but only manage to replace known chars, not random.

I already tried:

$file = file_get_contents('./tel6.out', true); 
$s=strtr($file, array( '' => '??' ));

Format of source-file:

enter image description here

  • 写回答

1条回答 默认 最新

  • dongmo20030416 2019-05-16 12:15
    关注
    $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("
    ", $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);
    

    Note: may happen the two random '?' are put in the same place for a chunk of string, so resulting only one '?' in a chunk of string. (Of course it can be prevented).

    It's not required to know the char to replace, if you overwrite it.

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧