doujumiao5024 2018-02-11 09:25
浏览 55
已采纳

如何在字符串中混音辅音?

In PHP, we know that we can randomly shuffle string characters using function str_shuffle(). So a string "developer" will become "lrevdeope", "dvolpeere" and so on every time.

But this is not the thing I want. Instead, I want to randomly shuffle only consonants. So "developer" should become "verelodep", "leveroped" etc. on each page refresh.

How can we achieve it? Any idea?

  • 写回答

4条回答 默认 最新

  • dsirr48088 2018-02-11 09:39
    关注

    After couple of minutes I have this:

    $s = 'developer';
    $cons = [];
    for ($i = 0; $i < strlen($s); $i++) {
        if (!in_array($s[$i], ['a', 'e', 'i', 'o', 'u', 'y'])) {
            $cons[] = $s[$i];
            $s[$i] = '-';
        }
    }
    
    shuffle($cons);
    
    for ($i = 0; $i < strlen($s); $i++) {
        if ($s[$i] == '-') {
            $s[$i] = array_shift($cons);
        }
    }
    
    echo $s . PHP_EOL;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”