Php 简单文本框架问题
基本上,我试图在文本周围创建一个框架,在文本所在的第三行,它不会打印出#,这应该是它周围的“填充”Php 简单文本框架问题,php,Php,基本上,我试图在文本周围创建一个框架,在文本所在的第三行,它不会打印出#,这应该是它周围的“填充” ****************** *################* *#HELLO, JUSTIN!* *################* ****************** 这就是它看起来的样子-在那之后!在$greeting变量中,应该有一个#。代码如下,有人能解释一下为什么会发生这种情况吗 <html> <body><?php $pad =
******************
*################*
*#HELLO, JUSTIN!*
*################*
******************
这就是它看起来的样子-在那之后!在$greeting变量中,应该有一个#。代码如下,有人能解释一下为什么会发生这种情况吗
<html>
<body><?php
$pad = 1;
$rows = ($pad * 2) + 3;
$greeting = "HELLO, JUSTIN!";
$col = strlen($greeting) + (2 * $pad) + 2;
for ($r = 0; $r != $rows; ++$r)
{
for ($c = 0; $c != $col; ++$c)
{
if ($r == $pad + 1 && $c == $pad + 1)
{
echo $greeting;
$c += strlen($greeting);
//echo "#";
}
else
{
if ($r == 0 || $r == $rows - 1 || $c == 0 || $c == $col - 1)
echo "*";
else
echo "#";
}
}
echo "<br />";
}
?></body>
</html>
您忘记容纳填充: 改变
$c += strlen($greeting);
到
必须替换为
$c += strlen($greeting)-1;
$greeting的垂直填充(上面和下面)很容易做到,最棘手的部分似乎是让包含$greeting的行正确显示
尝试使用这个答案是误导性的——在这个for循环中,后增量和前增量具有相同的效果。诚然,您必须从$greeting的长度中减去一,但您的回答暗示原因是递增(如果是这样,为什么不将其更改为post incr?)是的,我最好寻找一种不需要手动减去一的解决方案。我用C++ String,siz()函数编码C++中的完全相同的东西,不需要手工减去。我是初学者,不真正理解它会做什么,或者我将把它放在我的代码中。嗨,贾斯廷,很高兴听到您正在学习一些新的且具有挑战性的东西=)这实际上更像是一个学习挑战:让您的代码做同样的事情,但不要在任何for循环中更改$c或$r的值。搏击俱乐部的第一条规则:不要依附于你的代码。它是免费的(棒极了),你可以很容易地为自己编写一个迷宫(没那么棒),所以从头开始没什么大不了的。抱歉,我的答案很复杂,请在我更改它时等待。再次,寻找不需要手动编辑循环的真正解决方案。
$c += strlen($greeting);
$c += strlen($greeting)-1;
$text = "justin rocks my socks!";
$asteriskAt = 0;
$poundAt = 1;
$padding_left = 2;
$padding_right = 2;
//table looks like
//ABCDEFG
//HIJKLMN
//OPQRSTU
//row view
//0000000
//1111111
//3333333
//column view
//0123456
//0123456
//0123456
// ^ all the same "table" (anything with x and y, or rows and cols), just different ways of viewing it.
$num_cols = strlen($text) + ($padding_left + $padding_right); //so far so good?
//strlen($text) evaluates to 22
//$num_cols evaluates to 26 ( = 22 + 2 + 2)
for ($c = 0; $c <= $num_cols; ++$c) {
if ($c == $asteriskAt || $c == $num_cols - $asteriskAt) {
echo '*';
}
if ($c == $poundAt || $c == $num_cols - $poundAt) {
echo '#';
}
if ($c > $poundAt || $c < $num_cols - $poundAt) {
// is it clear why I picked this "range" ?
// if you do $c > 2 || $c < $num_cols - 2
// you will display something different
// because the cols and rows are zero-indexed
//
//cols
// 0123456
// ^2 is actually slot #3
//our thing will look like
//01___56
//compared with
//0123456
//01___56 <- (padding dudes)
//0123456
$idx = $c - $padding_left;
echo $text[$idx];
}
}
if (X || Y || Z || Q || P || NINE || WOLFHOUNDS || CATS || LION) {
//this will be incredibly hard to debug, so try to avoid it
//because it's basically a gateway, a CHANNEL into pandora's box
//and pandora's heart and soul
//and it's not fun in pandora's part of the universe
//although she is just trying to be happy like everyone else
//and we should not really discredit her opinions
//but man is this if statement going to be bothersome
//if one little thing goes wrong
//and we gotta figure out what caused it
}