PHP str_replace to replace need with random replac

2019-04-14 00:48发布

I've researched and need to find the best way to replace a need with an array of possibilities randomly.

ie:

$text = "Welcome to [city]. I want [city] to be a random version each time. [city] should not be the same [city] each time.";

$keyword = "[city]";
$values = array("Orlando", "Dallas", "Atlanta", "Detroit", "Tampa", "Miami");

$result = str_replace("[$keyword]", $values, $text);

The result is every occurrence has "Array" for city. I need to replace all of the city occurrences with a random from $values. I want to do this the cleanest way possible. My solution so far is terrible (recursive). What is the best solution for this? Thank you!

5条回答
放我归山
2楼-- · 2019-04-14 01:20

You're replacing a text using $values which is an array, so the result is just the word "Array". The replacement should be a string.

You can use array_rand() to pick random entries from your array.

$result = str_replace($keyword, $values[array_rand($values)], $text);

The result is something like this:

Welcome to Atlanta. I want Atlanta to be a random version each time. Atlanta should not be the same Atlanta each time.
Welcome to Orlando. I want Orlando to be a random version each time. Orlando should not be the same Orlando each time.

If you want the city to be random each line, check @PaulP.R.O's answer.

查看更多
Explosion°爆炸
3楼-- · 2019-04-14 01:21

You could use preg_replace_callback with array_rand

<?php
$text = "Welcome to [city]. I want [city] to be a random version each time. [city] should not be the same [city] each time.";

$values = array("Orlando", "Dallas", "Atlanta", "Detroit", "Tampa", "Miami");

$result = preg_replace_callback("/\[city\]/", function($matches) use ($values) { return $values[array_rand($values)]; }, $text);

echo $result;

Example here.

查看更多
闹够了就滚
4楼-- · 2019-04-14 01:22

Here's another idea

$text = "Welcome to [city]. I want [city] to be a random version each time. [city] should not be the same [city] each time.";

$pattern = "/\[city\]/";
$values = array("Orlando", "Dallas", "Atlanta", "Detroit", "Tampa", "Miami");

while(preg_match($pattern, $text)) {
        $text = preg_replace($pattern, $values[array_rand($values)], $text, 1);
}

echo $text;

And some output:

Welcome to Orlando. I want Tampa to be a random version each time. Miami should not be the same Orlando each time.
查看更多
小情绪 Triste *
5楼-- · 2019-04-14 01:31

try this http://codepad.org/qp7XYHe4

<?
$text = "Welcome to [city]. I want [city] to be a random version each time. [city] should not be the same [city] each time.";

$keyword = "[city]";
$values = array("Orlando", "Dallas", "Atlanta", "Detroit", "Tampa", "Miami");

echo $result = str_replace($keyword, shuffle($values)?current($values):$values[0], $text);
查看更多
6楼-- · 2019-04-14 01:40

You can use preg_replace_callback to execute a function for each match and return the replacement string:

$text = "Welcome to [city]. I want [city] to be a random version each time. [city] should not be the same [city] each time.";

$keyword = "[city]";
$values = array("Orlando", "Dallas", "Atlanta", "Detroit", "Tampa", "Miami");

$result = preg_replace_callback('/' . preg_quote($keyword) . '/', 
  function() use ($values){ return $values[array_rand($values)]; }, $text);

Sample $result:

Welcome to Atlanta. I want Dallas to be a random version each time. Miami should not be the same Atlanta each time.

查看更多
登录 后发表回答