我需要在 php 页面中整齐地输出旋转文本。我已经有了 {hi|hello|greetings} 格式的预纺文本。我有一个我在其他地方找到的 php 代码,但它不会在句子级别输出旋转文本,其中两个 {{ 来了。这是需要修复的代码。
<?php
function spinText($text){
$test = preg_match_all("#\{(.*?)\}#", $text, $out);
if (!$test) return $text;
$toFind = Array();
$toReplace = Array();
foreach($out[0] AS $id => $match){
$choices = explode("|", $out[1][$id]);
$toFind[]=$match;
$toReplace[]=trim($choices[rand(0, count($choices)-1)]);
}
return str_replace($toFind, $toReplace, $text);
}
echo spinText("{Hello|Hi|Greetings}!");;
?>
输出将随机选择单词:Hello OR Hi OR Greetings。
但是,如果有一个句子级别的旋转,输出就会混乱。例如:
{{hello|hi}.{how're|how are} you|{How's|How is} it going}
输出是
{hello.how're you|How is it going}
如您所见,文本尚未完全旋转。
谢谢