+
95
-

php如何实现大小写随机转换?

php

php如何实现大小写随机转换?

例如将baidu.com可以随机转换成BaIdOM.cOm等其他形式。

网友回复

+
15
-

要在PHP中实现字符串的大小写随机转换,可以使用以下步骤:

遍历字符串的每个字符随机决定每个字符是大写还是小写构建新的字符串

以下是一个示例代码,展示了如何实现这一功能:

<?php
function randomCaseConversion($input) {
    $output = '';
    $length = strlen($input);

    for ($i = 0; $i < $length; $i++) {
        // 随机决定当前字符是大写还是小写
        if (rand(0, 1) === 0) {
            $output .= strtolower($input[$i]);
        } else {
            $output .= strtoupper($input[$i]);
        }
    }

    return $output;
}

//...

点击查看剩余70%

我知道答案,我要回答