santa
santa

Reputation: 12512

Replace words in a a string randomly

I'm trying to write a script that will take a text string and will allow me replace random words. For example:

$str = "The quick brown fox jumps over the lazy dog";

I will out put as and replace couple words like this:

The quick ______ fox jumps over the ____ dog

I can probably do this by first splitting the string into array

$arr = str_word_count($str, 1);

And then replace $arr[2] and $arr[7].

The issue that I think I'll have if there are non-words in the string, like punctuation:

$str = "The quick brown fox, named Jack, jumps over the lazy dog; and Bingo was his...";

How do I go about resolving this? Ideas?

Upvotes: 3

Views: 159

Answers (2)

TheBlueOne
TheBlueOne

Reputation: 496

I think a much better apporach would be to use a regex, because you don't just allow commas, but everything which is not a word character. Also regexes are much faster than normal splittings or substrings in loops. My Solution would be:

<?php
function randomlyRemovedWords($str)
{
    $sentenceParts = [];

    $wordCount = preg_match_all("/([\w']+)([^\w']*)/", $str, $sentenceParts, PREG_SET_ORDER);

    for ($i = 0;$i < $wordCount / 4;$i++)
    { //nearly every fourth word will be changed
        $index = rand(0, $wordCount - 1);

        $sentenceParts[$index][1] = preg_replace("/./", "_", $sentenceParts[$index][1]);
    }

    $str = "";
    foreach ($sentenceParts as $part)
    {
        $str .= $part[1] . $part[2];
    }

    return $str;
}

echo randomlyRemovedWords("The quick brown fox, doesn't jumps over, the lazy dog.");
echo "\n<br>\n";
echo randomlyRemovedWords("The quick brown fox, jumps over, the lazy dog.");

which results in

The quick brown ___, _______ jumps over, the ____ dog.
<br>
The quick brown fox, jumps ____, ___ ____ dog.

This way you can be sure to ignore all nonword characters and remove words randomly.

Upvotes: 2

ikiK
ikiK

Reputation: 6532

You can do it like this:

   $test1 = "test1";
    $test2 = "test2";
    $test3 = "Bingo2";
    // set new words


    $str = "The quick brown fox, named Jack, jumps over the lazy dog; and Bingo was his...";
    $re = explode(" ", $str);
    // split them on space in array $re
    echo $str  . "<br>";
    $num = 0;

    foreach ($re as $key => $value) {
        echo $value . "<br>";
        $word = "";

        switch (true) {
            case (strpos($value, 'Jack') !== false):
                // cheak if each value in array has in it wanted word to replace 
                // and if it does
                $new = explode("Jack", $value);
                // split at that word just to save punctuation
                $word = $test1 . $new[1];
                //replace the word and add back punctuation
                break;
            case (strpos($value, 'dog') !== false):
                $new1 = explode("dog", $value);
                $word = $test2 . $new1[1];
                break;
            case (strpos($value, 'Bingo') !== false):
                $new2 = explode("Bingo", $value);
                $word = $test3 . $new2[1];
                break;
            default:
                $word = $value;
                // if no word are found to replace just leave it
        }

        $re[$num++] = $word;
        //push new words in order back into array
    };


    echo  implode(" ", $re);
        // join back with space

Result:

The quick brown fox, named test1, jumps over the lazy test2; and Bingo2 was his... 

It works with or without punctuation.

But keep in mind if you have Jack and Jacky for example you will need to add additional logic such as checking if punctuation part does not have any letters in it with Regex to match only letters, if it does skip it, it means it was not full match. Or soothing similar.

EDIT (based on comments):

$wordstoraplce = ["Jacky","Jack", "dog", "Bingo","dontreplace"];
$replacewith = "_";
$word = "";
$str = "The quick brown fox, named Jack, jumps over the lazy dog; and Bingo was his...";
echo $str . "<br>";
foreach ($wordstoraplce as $key1 => $value1) {
    $re = explode(" ", $str);
    foreach ($re as $key => $value) {
        if((strpos($value, $value1) !== false)){
            $countn=strlen($value1);
            $new = explode($value1, $value);
            if (!ctype_alpha ($new[1])){
                $word = " " . str_repeat($replacewith,$countn) . $new[1]. " ";
            }else{
                $word = $value;
            }
        }else{
            $word = $value;
        };
        //echo  $word;  
        $re[$key] = $word;      
    };
    $str =  implode(" ", $re);
};
echo $str;

RESULT:

The quick brown fox, named Jack, jumps over the lazy dog; and Bingo was his...
The quick brown fox, named ____, jumps over the lazy ___; and _____ was his... 

Upvotes: 2

Related Questions