0

I am developing a script that takes an article, searches the article for a "keyword" and then randomly replaces that keyword with an anchor link.

I have the script working as it should, however I need to be able to have an array of "replacements" for the function to loop through and insert at the random location.

So the first random position would get array value 1. The second random position would get array value 2. The third random position would get array value 3. etc...

public function replace_random ($str, $search, $replace, $n) {

    // Get all occurences of $search and their offsets within the string
    $count = preg_match_all('/\b'.preg_quote($search, '/').'\b/', $str, $matches, PREG_OFFSET_CAPTURE);

    // Get string length information so we can account for replacement strings that are of a different length to the search string
    $searchLen = strlen($search);
    $diff = strlen($replace) - $searchLen;
    $offset = 0;

    $searchCount = count($replace);
    $arrayNum = mt_rand(0, 4);

    // Loop $n random matches and replace them, if $n < 1 || $n > $count, replace all matches
    $toReplace = ($n < 1 || $n > $count) ? array_keys($matches[0]) : (array) array_rand($matches[0], $n);
    foreach ($toReplace as $match) {
        $str = substr($str, 0, $matches[0][$match][1] + $offset).$replace[$arrayNum].substr($str, $matches[0][$match][1] + $searchLen + $offset);
        $offset += $diff;
    }

    return $str;

}

So my question is, How can i alter this function to accept the array for the $replace variable?

2
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by tpickett
0

@tpickett

I have the script working as it should, however I need to be able to have an array of "replacements" for the function to loop through and insert at the random location.

I don't understand what you are trying to do? You didn't show the array?

Which of these are the array: $str, $search, $replace, $n?

Edited by LastMitch: grammer

0

$replace is the array. and it would be structured like this:

$replace = array(
    'racer',
    'race car driver',
    'racing',
    'race team'
    );//array to replace $match with

$search = "text"; //keyword to match

n = "3"; //number of times to replace in artcle 

$str = 'this is example text. it has text. wouldnt you like to replace this text? text is amazing';//string to analyze
0

@tpickett

$replace is the array.

$replace = array('racer','race car driver','racing','race team'); //array to replace $match with

$search = "text"; //keyword to match

n = "3"; //number of times to replace in artcle 

$str = 'this is example text. it has text. wouldnt you like to replace this text? text is amazing';//string to analyze

You want this:

The 1st random position would get array value 1.
The 2nd random position would get array value 2.
The 3rd random position would get array value 3.

The $n doesn't make sense in here:

 $toReplace = ($n < 1 || $n > $count) ? array_keys($matches[0]) : (array) array_rand($matches[0], $n);
    foreach ($toReplace as $match) {
        $str = substr($str, 0, $matches[0][$match][1] + $offset).$replace[$arrayNum].substr($str, $matches[0][$match][1] + $searchLen + $offset);

Edited by LastMitch: grammer

0

the $n makes sure that the function doesn't replace more that what was set to be replaced.

So if I have a string with 6 matches of $search, and $n is set to 3, then the function only replaces 3 of those 6 matches at random.

0

What you doing now is not what you are asking?

You want this:

The 1st random position would get array value 1.
The 2nd random position would get array value 2.
The 3rd random position would get array value 3.

But you're intention is this:

So if I have a string with 6 matches of $search, and $n is set to 3, then the function only replaces 3 of those 6 matches at random.

I hope that make sense. I'm reading your CODE not what you are asking.

Edited by LastMitch: grammer

0

you are correct the code is functioning that way currently.

What i would like to happen is the 3 matches that get replaced, be replaced with differen't values(chosen at random from the $replace array). It currently outputs only 1 value for all 3 matches.

my original attempt is this part:

$searchCount = count($replace);
    $arrayNum = mt_rand(0, 4);//the last parameter should be $replace, but i set staic values trying to get it to work.

and the function runs in a loop.

hope that makes sense...

Edited by tpickett: explanation

0

i figured it out by adding in this line:

$replaceRand    =   $replace[array_rand($replace)];

into the foreach loop.

Thanks for your help!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.