可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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!
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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);
回答5:
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.