Issue with simple text framing
Basically, I'm trying to create a frame around text and on the third line where the text goes, it doesn't print out the # that's supposed to be the "padding" around it.
******************
*################*
*#HELLO, JUSTIN!*
*################*
******************
That's what it looks like - after the ! in the $greeting variable, th开发者_StackOverflow社区ere should be a #. The code is below, could anyone explain why this happens?
<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>
The vertical padding (above and below) the $greeting is easy to do, the trickiest part seems to be getting the row containing $greeting to display right.
Try using <= (less-than-or-equal-to) in your for-loop, instead of != (not-equal[s]). You will have to change the ranges on your if statements.
$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];
}
}
By the way, www.ideone.com has a PHP interpreter, too! awesome.
Check out the differences in these 2 outputs, and notice that I only changed the one operator in the for-loop:
http://ideone.com/M1lpM //asterisk :D
http://ideone.com/JqsTx //no last asterisk =(
the code you posted is not bad, especially if it works on the first try! but really, programming and software in general demands precision, and you can't get angry at a computer for doing what you tell it. Thus, it's much better to work in stages or steps, for when you could use clarity in debugging =)
Programmers learn gradually (if they are stubborn like me) that it's not only about having the code work 2 minutes from when you started writing it, it's about having it be readable and change-able, maintainable in the future. It's a good habit to develop, since you never know how long it will be before you look at your code again "what the heck was this crazy person doing?!" you might think, then realize you wrote it a year ago ...and you never know who will end up maintaining whatever you make, whether you contribute to an open source project or work on a commercial application.
So, some suggestions (that I would tell my 6-years-ago-self, really) :
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
}
If you have any questions about the code I posted, don't hesitate to ask =)
hopefully this helps you figure out why the code you posted doesn't work the way you expected it to, but you can very easily make a Gordian Knot that is very difficult to cut through without perfect wisdom / a life of meditation and moderation
$c += strlen($greeting);
must be replaced with
$c += strlen($greeting)-1;
You forgot to accommodate for the padding:
Change
$c += strlen($greeting);
to
$c += strlen($greeting) - $pad;
精彩评论