原因的Spintax类取代的所有实例{spintax | spuntext}具有相同的随机选择的选项是因为该线在类的:
$str = str_replace($match[0], $new_str, $str);
的str_replace
函数替换所有实例在搜索字符串中替换的子字符串。要取代只有第一个实例,按照您所希望的顺序进行,我们需要使用函数preg_replace
,并且传递的“count”参数为1.但是,当我查看Spintax类的your link并引用post #7我注意到他对Spintax课程的建议增强有一个错误。
fransberns建议更换:
$str = str_replace($match[0], $new_str, $str);
与此:
//one match at a time
$match_0 = str_replace("|", "\|", $match[0]);
$match_0 = str_replace("{", "\{", $match_0);
$match_0 = str_replace("}", "\}", $match_0);
$reg_exp = "/".$match_0."/";
$str = preg_replace($reg_exp, $new_str, $str, 1);
与fransbergs'建议的问题是,在他的代码,他没有正确构建的正则表达式的preg_replace
功能。他的错误来自于没有正确逃脱\
角色。接替他的代码应该是这个样子的:
//one match at a time
$match_0 = str_replace("|", "\\|", $match[0]);
$match_0 = str_replace("{", "\\{", $match_0);
$match_0 = str_replace("}", "\\}", $match_0);
$reg_exp = "/".$match_0."/";
$str = preg_replace($reg_exp, $new_str, $str, 1);
考虑利用我校在fransberns'建议replacemnet:
class Spintax {
function spin($str, $test=false)
{
if(!$test){
do {
$str = $this->regex($str);
} while ($this->complete($str));
return $str;
} else {
do {
echo "<b>PROCESS: </b>";var_dump($str = $this->regex($str));echo "<br><br>";
} while ($this->complete($str));
return false;
}
}
function regex($str)
{
preg_match("/{[^{}]+?}/", $str, $match);
// Now spin the first captured string
$attack = explode("|", $match[0]);
$new_str = preg_replace("/[{}]/", "", $attack[rand(0,(count($attack)-1))]);
// $str = str_replace($match[0], $new_str, $str); //this line was replaced
$match_0 = str_replace("|", "\\|", $match[0]);
$match_0 = str_replace("{", "\\{", $match_0);
$match_0 = str_replace("}", "\\}", $match_0);
$reg_exp = "/".$match_0."/";
$str = preg_replace($reg_exp, $new_str, $str, 1);
return $str;
}
function complete($str)
{
$complete = preg_match("/{[^{}]+?}/", $str, $match);
return $complete;
}
}
当我试图用fransberns'这个增强版本替换原始类建议替换为“原样”,由于\
字符不正确的转义,我得到了一个无限循环。我认为这是你的记忆问题来自的地方。在纠正fransberns'建议的替换与\
字符的正确转义我没有进入无限循环。
尝试使用更正的扩充以上的类,看看它是否在你的服务器上工作(我看不出它不应该这样做的原因)。
附注:你真的想要它是随机的吗?或者你正在寻找不同的输出?如果它是随机的,完全有可能有许多重复的单词。 – HappyTimeGopher 2012-08-03 22:27:13
我现在还没有机会写+测试它,但是您需要对preg_replace应用一个限制,并在目标单词仍在文档中时循环。添加一个计数器并打破之后,说20个循环以避免无限循环。 – HappyTimeGopher 2012-08-03 22:31:19
@HappyTimeGopher - 是的,随机的。我很好,在上面的情况下,“spintax”重复3次,一次又一次,而不是每次运行该函数的时间;) – Kane 2012-08-03 22:36:34