- preg_replace_callback: documentation ( source)
- shuffle: documentation ( source)
- preg_quote: documentation ( source)
- array_pop: documentation ( source)
<?php
$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 = ["Orlando", "Dallas", "Atlanta", "Detroit", "Tampa", "Miami"];
shuffle($values);
echo preg_replace_callback(
'/' . preg_quote($keyword) . '/',
function() use(&$values) { return array_pop($values); },
$text
);