Php 在表格中插入随机字母和字符串字母,就像在wordsearch中一样

Php 在表格中插入随机字母和字符串字母,就像在wordsearch中一样,php,arrays,tcpdf,Php,Arrays,Tcpdf,这段代码是用来用表格显示随机字母的,就像在单词搜索游戏中一样。问题是,。我无法在表中插入字符串,。如果我有一个字符串数组,。例如,样品、它们、电源、一些。如何在单元格中插入这些字符串 我正在创造这样的东西 $row=10; $col=10; $w=8; $h=8; 对于($r=1;$rLn($h); } 试试这段代码 $row = 10; $col = 10; $w = 8; $h = 8; $characters = range('A','Z'); $max = count($charac

这段代码是用来用表格显示随机字母的,就像在单词搜索游戏中一样。问题是,。我无法在表中插入字符串,。如果我有一个字符串数组,。例如,样品、它们、电源、一些。如何在单元格中插入这些字符串

我正在创造这样的东西

$row=10;
$col=10;
$w=8;
$h=8;
对于($r=1;$rLn($h);
}
试试这段代码

$row = 10;
$col = 10;

$w = 8;
$h = 8;

$characters = range('A','Z');
$max = count($characters) - 1;  

for ($r=1;$r<=$row;$r++) {
    for ($c=1;$c<=$col;$c++) {
        $pdf->Cell($w,$h,$characters[mt_rand(0,$max)],1,0,'C');

        //for debugging (remove this)
        echo("$w,$h,{$characters[mt_rand(0,$max)]},1,0,'C'");
        echo('<br>');
    }
    $pdf->Ln($h);
}

对于stackoverflow来说,这远远超出了预期,但我的心情很好。:-)

因此,这需要一个由comas分隔的字符串(您可以添加更多)并将其拆分为一个数组,然后找到适合的单词,直到它减少到1个字母,然后它只使用之前的随机字母填充空间

这远不是完美的,你会看到所有的行都以单词开头,以随机字母结尾。而且,这些词只是水平的,而不是垂直的。构建单词水平、垂直和对角线的矩阵变得相当复杂,肯定超出了本网站的范围

这是目前的代码。这是一个好的开始

$row = 10;
$col = 10;

$w = 8;
$h = 8;
$characters = range('A','Z');

$words="in, on, it, up, at, am, of, and, band, banned, bland, brand, brande, canned, chand, fanned, gland, grand, grande, hand, land, lande, mande, manned, panned, planned, rand, sande, scanned, shand, spanned, stand, strand, strande, tanned, vande, zand";

$wordList = explode(",",$words);
$myWord="";

$max = count($characters) - 1;  
for ($r=1;$r<=$row;$r++) {
    for ($c=1;$c<=$col;$c++) {
        //if we have more than 1 space
        //(enough room for a 2 or more letter word)
        $maxWordLength=$col-$c+1;
        if($maxWordLength > 1){
            //get a word if we need a new one
            if(empty($myWord)){
                $wordLen=99;
                while($wordLen > $maxWordLength){
                    $myWord=trim(strToUpper($wordList[mt_rand(0,count($wordList))]));
                    $wordLen=strlen($myWord);
                }
            }
            //get the next letter of the word we're using
            $thisChar=substr($myWord,0,1);
            $myWord=substr($myWord,1);
        }else{
            //if we don't have room for a word put a random letter
            $thisChar=$characters[mt_rand(0,$max)];
        }


        $pdf->Cell($w,$h,$thisChar,1,0,'C');

        //for debugging (remove this)
        echo($thisChar.' ');

    }
    $pdf->Ln($h);

    //for debugging remove this
    echo('<hr>');
}
$row=10;
$col=10;
$w=8;
$h=8;
$characters=范围('A','Z');
$words=“in,on,it,up,at,am,of,and,band,band,band,band,bande,bland,brande,canned,chand,fanned,gland,grand,grande,hand,land,lande,mande,mande,mand,panned,planning,rand,sande,scanned,shand,span,stand,strand,strand,strand,tande,tande,tande,vande,zand”;
$wordList=explode(“,”,$words);
$myWord=“”;
$max=计数($characters)-1;
对于($r=1;$r$maxWordLength){
$myWord=trim(strToUpper($wordList[mt_rand(0,count($wordList))));
$wordLen=strlen($myWord);
}
}
//获取我们正在使用的单词的下一个字母
$thisChar=substr($myWord,0,1);
$myWord=substr($myWord,1);
}否则{
//如果我们没有地方放一个字,就随便放一个字母
$thisChar=$characters[mt_rand(0,$max)];
}
$pdf->Cell($w,$h,$thisChar,1,0,'C');
//用于调试(删除此项)
echo($thisChar.');
}
$pdf->Ln$h;
//要进行调试,请删除此
回声(“
”); }
此外,这里还有一个链接,指向一个现有的开放源码PHP解决方案,该解决方案符合您的需求。我下载了它,看了一眼,大概有3000行代码。正如我所说的,这远远超出了堆栈溢出的范围


为什么要调用数组\u merge?(我的意思是range已经返回了一个数组,在一个数组上调用array_merge没有任何作用)而且据我所知,你永远不会在任何地方使用变量$characters或#max。还有其他代码吗?还有,您遇到了什么错误或不希望出现的行为?代码中没有错误,问题是,我需要插入string.Oppps.的字母。很抱歉,当我删除数组时,请忽略变量$max和$character,。我得到了错误,。但当它有数组_merge时没有错误。所以没问题。很好,谢谢你修改我的代码,你的代码和我的代码有相同的输出。那么,如何在这里输入一个字符串和随机字母呢?例如好了,数据,不知怎么的。哦,现在我明白你的意思了。您可能需要一个字典文件或其他东西,但您可以使用一个单词数组。但它们不再是随机的字母了。我马上再提交。@user2901740刚刚添加了另一个答案,其中包含您想要的代码以及指向完整解决方案的链接。如果这回答了您的问题,请将其标记为这样。谢谢。等等,我会测试这个:)嗨,我测试了你的代码,它有一个小错误,它产生了两到三个空白框。我只用了4个单词来测试这个。但是thx。我将尝试使用您的代码(thx:)
8,8,D,1,0,'C'
8,8,S,1,0,'C'
8,8,C,1,0,'C'
8,8,C,1,0,'C'
8,8,F,1,0,'C'
8,8,Q,1,0,'C'
8,8,B,1,0,'C'
8,8,H,1,0,'C'
8,8,C,1,0,'C'
8,8,N,1,0,'C'
8,8,A,1,0,'C'
8,8,H,1,0,'C'
8,8,Y,1,0,'C'
8,8,T,1,0,'C'
8,8,B,1,0,'C'
8,8,V,1,0,'C'
8,8,Y,1,0,'C'
...
$row = 10;
$col = 10;

$w = 8;
$h = 8;
$characters = range('A','Z');

$words="in, on, it, up, at, am, of, and, band, banned, bland, brand, brande, canned, chand, fanned, gland, grand, grande, hand, land, lande, mande, manned, panned, planned, rand, sande, scanned, shand, spanned, stand, strand, strande, tanned, vande, zand";

$wordList = explode(",",$words);
$myWord="";

$max = count($characters) - 1;  
for ($r=1;$r<=$row;$r++) {
    for ($c=1;$c<=$col;$c++) {
        //if we have more than 1 space
        //(enough room for a 2 or more letter word)
        $maxWordLength=$col-$c+1;
        if($maxWordLength > 1){
            //get a word if we need a new one
            if(empty($myWord)){
                $wordLen=99;
                while($wordLen > $maxWordLength){
                    $myWord=trim(strToUpper($wordList[mt_rand(0,count($wordList))]));
                    $wordLen=strlen($myWord);
                }
            }
            //get the next letter of the word we're using
            $thisChar=substr($myWord,0,1);
            $myWord=substr($myWord,1);
        }else{
            //if we don't have room for a word put a random letter
            $thisChar=$characters[mt_rand(0,$max)];
        }


        $pdf->Cell($w,$h,$thisChar,1,0,'C');

        //for debugging (remove this)
        echo($thisChar.' ');

    }
    $pdf->Ln($h);

    //for debugging remove this
    echo('<hr>');
}