我是PHP的新手。
我想用PHP创建一篇文章微调器。
在使用 spintax 函数进行字符串处理之前,我想用其同义词替换一些文本。
E.G:
$text = "Mother fry a turkey in the cooking room accompanied by a father who makes drinks and mother really happy.";
结果:
$newstring = "{mother|mama} {fry|cook} {a turkey|the chicken} in the {the cooking room|kitchen} {accompanied by|with} {father|papa} who {makes|prepare} {coffee|drinks} and {mother|mama} really {happy|fun}.";
我不使用mysql数据库,但只使用txt文件,只使用fopen获取同义词。
但结果非常糟糕,
E.G:
$newstring = "{mother|mama} {fry|cook} {a turkey|the chicken} in the {the cooking room|kitchen} {accompanied by|with} {father|papa} who {makes|prepare} {coffee|drinks} and {{mother|mama}|mama} really {happy|fun}.";
我使用这样的正则表达式:
$text = preg_replace( "/\b$search\b/sui", $replace, $text );
我应该使用什么正则表达式来避免重复替换?
任何人都可以帮助我?
抱歉我的英语不好。
先谢谢。
答案 0 :(得分:0)
让我们摆脱旧思维模式:
$text = "Father roasts mother in the cooking room accompanied by a turkey who makes drinks and the turkey is really happy.";
您需要一个关联数组并构建一个替换以匹配列表中的每个单词,并使用preg_replace_callback
一次性替换所有单词:
$corr = [ 'father' => '{father|daddy}', 'roasts' => '{roasts|cooks}', 'cooking room' => '{cooking room|kitchen}' ];
$words = array_keys($corr);
rsort($words);
$pattern = '~\b(?:' . implode('|', $words) . ')\b~ui';
$result = preg_replace_callback($pattern, function ($m) use ($corr) {
$word = strtolower($m[0]); // better with mb_strtolower() if available
return $corr[$word];
}, $text);
u修饰符强制正则表达式引擎将字符串读取为以UTF-8编码的unicode字符串(如果字符串使用ASCII范围之外的字符,则非常有用)。
请注意,以相反的顺序对单词数组进行排序非常重要,因为正则表达式引擎会返回在交替中成功的第一个分支。