我希望这个问题没有在另一个线程中得到解决 - 做了一些搜索但却一无所获.
我正在编写一个函数来以字符串的形式创建一些随机注释.它将用于生产中的循环上下文中.我写了一个while循环来测试函数,我得到了一些奇怪的输出.它在第一个循环中运行良好,但每个后续循环都将字符串截断为它们的第一个字符.
<?PHP
$prefix=array();
$prefix[]="Wow! That's";
$prefix[]="That's";
//...
$prefix[]="Amazing image. So";
$prefix[]="Wonderful image. So";
$suffix=array();
$suffix[]="amazing";
$suffix[]="awesome";
//...
$suffix[]="fabulous";
$suffix[]="historic";
$punctuation=array();
$punctuation[]='!';
$punctuation[]='!!';
//...
$punctuation[]='.';
$punctuation[]='...';
function comment() {
global $prefix;
$prefix_max=count($prefix)-1;
$rand=rand(0,$prefix_max);
$prefix=$prefix[$rand];
global $suffix;
$suffix_max=count($suffix)-1;
$rand=rand(0,$suffix_max);
if(strpos(strtolower($prefix),strtolower($suffix[$rand])) > 0) {
$rand=$rand+1;
if($rand > $suffix_max) {
$rand=0;
}
}
$suffix=$suffix[$rand];
if(substr($prefix, -1) == '.' || substr($prefix, -1) == '!') {
$suffix=ucfirst($suffix);
}
$rand=rand(1,100);
if($rand < 18) {$suffix=strtoupper($suffix);}
global $punctuation;
$punctuation_max=count($punctuation)-1;
$rand=rand(0,$punctuation_max);
$punctuation=$punctuation[$rand];
$comment=$prefix.' '.$suffix.$punctuation;
return $comment;
} …Run Code Online (Sandbox Code Playgroud)