如何使用PHP将随机字母插入句子中的特定单词

如何使用PHP将随机字母插入句子中的特定单词,php,Php,这是我的问题,我想做一个php函数,在特定单词中插入一些随机字母 例如: 随机字母:a、i、u、e、o 具体单词:猴子、鹿、老虎、老鼠 老虎吃鹿,猴子和老鼠从远处看 $sentence = 'a tiger eating a deer, monkeys and rats see from a distance'; // You'll need to load the string into a variable, obviously. $randomLetterArray = array('a

这是我的问题,我想做一个php函数,在特定单词中插入一些随机字母

例如:

随机字母:a、i、u、e、o

具体单词:猴子、鹿、老虎、老鼠

老虎吃鹿,猴子和老鼠从远处看

$sentence = 'a tiger eating a deer, monkeys and rats see from a distance'; // You'll need to load the string into a variable, obviously.

$randomLetterArray = array('a', 'e', 'i', 'o', 'u'); // It's good to set the random letters inside an array so that they can be randomly picked by randomizing the index number.

$specificWordArray = array('monkey', 'deer', 'tiger', 'mouse'); // Words to be altered in an array to easily iterate through.

foreach ($specificWordArray as $specificWord) // Iterating through each of the words to be altered.
{
    $indexOfWord = rand(0, strlen($specificWord)-1); // Getting a random number between 0 and the length (in chars) of the word.
    $partOfWord = substr($specificWord, 0 , $indexOfWord); // Getting a part of the word based on the random number created above.
    // This is more complex.
    // The part of the word created above is concatenated by a random selection of one of the random letters.
    // This in turn is concatenated by the remaining letters in the word.
    $wordReplacement = $partOfWord.$randomLetterArray[rand(0, count($randomLetterArray)-1)].substr($specificWord, $indexOfWord);
    $sentence = str_ireplace($specificWord, $wordReplacement, $sentence); // This replaces the word in the sentence with the new word.
}
echo $sentence;
我希望这句话是这样的:

一只食肉恐龙,猴子和兔子从远处看

$sentence = 'a tiger eating a deer, monkeys and rats see from a distance'; // You'll need to load the string into a variable, obviously.

$randomLetterArray = array('a', 'e', 'i', 'o', 'u'); // It's good to set the random letters inside an array so that they can be randomly picked by randomizing the index number.

$specificWordArray = array('monkey', 'deer', 'tiger', 'mouse'); // Words to be altered in an array to easily iterate through.

foreach ($specificWordArray as $specificWord) // Iterating through each of the words to be altered.
{
    $indexOfWord = rand(0, strlen($specificWord)-1); // Getting a random number between 0 and the length (in chars) of the word.
    $partOfWord = substr($specificWord, 0 , $indexOfWord); // Getting a part of the word based on the random number created above.
    // This is more complex.
    // The part of the word created above is concatenated by a random selection of one of the random letters.
    // This in turn is concatenated by the remaining letters in the word.
    $wordReplacement = $partOfWord.$randomLetterArray[rand(0, count($randomLetterArray)-1)].substr($specificWord, $indexOfWord);
    $sentence = str_ireplace($specificWord, $wordReplacement, $sentence); // This replaces the word in the sentence with the new word.
}
echo $sentence;
我希望这能让您了解如何解决这个问题,并且我希望这些评论能帮助您了解PHP中可用的函数


祝你好运

像这样的方法应该行得通,但我确信有一种更快更简单的方法。
# the magic function
function magicHappensHere ( $letters, $words, $sentence ) {

    $return = '';

    # divide by words
    $sentence_words = explode( ' ', $sentence );

    foreach ( $sentence_words as $value ) {

        # check words
        if ( array_search( $value, $words ) ) {
            # check 'letter' count
            $l_number = sizeof( $letters );

            # create a random letter value
            $rand_letter = rand( 0, ( $l_number-1 ) );

            # create random position to use
            $rand_pos = rand ( 0, strlen( $value ) );

            # change the actual word
            $value = substr_replace( $value, $letters[ $rand_letter ] ,$rand_pos, 0 );
        }

        $return .= $value . ' ';

    }

    # print out new string ( w/out last space )
    return substr( $return, 0, -1 );
}

# define your 'letters'
$letters = array( 'a', 'i', 'u', 'e', 'o' );

# define your 'words'
$words = array( 'monkey', 'deer', 'tiger', 'mouse' );

# define your 'sentence'
$sentence = 'a tiger eating a deer, monkeys and rats see from a distance';

# change text
$new_sentence = magicHappensHere ( $letters, $words, $sentence );

print $new_sentence;

?>

人们想干些什么稀奇古怪的事呢。请记住,这是一个获取特定编程问题帮助的地方,而不是征求他人为您编写代码的地方。您的代码仅更改第一个单词。但感谢您的帮助:)