Hi guys
I'm new to php. I've been given a task to write a code in php that would allow the user to input his initials and the program should out his/her initials as a star pattern. E.g if your initials T.T,it should be as follows...

*****   *****
  *       *     
  *       *
  *       *

Please help!! Have no idea where to start!!

This certainly isn’t a homework for absolute newbie’s (I guess that the answer that the one asked will give will prove that has none or basic knowledge of programming) . (but to be honest I am not here to help students with their homework but to help programmers) . One simple way is creating an image with that text and then read each pixel and put “ ” or “*” in the output depending if that pixel has color or is blank.

Member Avatar for diafol

Intrigued by this, I thought you could store a 5x5 pixel (25-bit) character as an integer. So...

For A:

1,1,1,1,1, 1,0,0,0,1, 1,0,0,0,1, 1,1,1,1,1, 1,0,0,0,1 

This equates to the integer: 18859583

Based on the pixel bit increment:

1,2,4,8,16,32...

if working from pixel1 = 1, pixel2= 2, pixel3 = 4 etc etc

Here's a screenshot of proof of concept...

bdd7d54ebcfa0b2c5c057b47ee7f6563

commented: you know you're gonna end up writing the rest of the code :-) +6
Member Avatar for diafol

he he Paul. I'm done with it. I dangled the carrot, it's up to the OP.

Although, it took only about 10 minutes to knock this up in total...

02465bf6fb02a31c3f981f91825f06ca

So, it should be do-able in a reasonable time for a beginner

Thanks guys!
I'm gonna try this out and let you know where i get with this

Member Avatar for diafol

Remember - as this is your work, ask away when you get stuck. :)

Well, i'm extremely curious...and I'm stuck.

I'm at the point where i understand the general concept and that your set of numbers can be stored as a binary number (right to left), converted to a integer?

[1,1,1,1,1, 1,0,0,0,1, 1,0,0,0,1, 1,1,1,1,1, 1,0,0,0,1] would be stored in binary as 1000111111100011000111111 which does equal to 18859583 in base10 (decimal).

So I can simply draw out the alphabet to figure out what the binary/decimal equivalents are for each letter.

Then what? When I type a sentence, i need to parse out each letter and I'm stuck here...how are the letters displayed next to each other?

Am I on the right track? I hope I'm not letting out any secrets...

Member Avatar for diafol

Yep fine...

You can't type the complete topline of dots for the whole sentence, then the next, then the next (5 lines in all) as overflow page width will cause horribleness! So enclose each letter in an inline-block div. Also I implode the separate characters with a thin div (single column of dots) with a separator so that characters don't join onto each other.

I used the • character for 1 and   for 0.

BTW - it's probably easier to read the binary than to convert to integer - that was just me playing ;)

Here's my particular 5x5 font:

$alphabet = array(
    'A'=>   array(1,1,1,1,1, 1,0,0,0,1, 1,0,0,0,1, 1,1,1,1,1, 1,0,0,0,1),
    'B'=>   array(1,1,1,1,1, 1,0,0,0,1, 1,1,1,1,0, 1,0,0,0,1, 1,1,1,1,1),
    'C'=>   array(1,1,1,1,1, 1,0,0,0,0, 1,0,0,0,0, 1,0,0,0,0, 1,1,1,1,1),
    'D'=>   array(1,1,1,1,0, 1,0,0,0,1, 1,0,0,0,1, 1,0,0,0,1, 1,1,1,1,0),
    'E'=>   array(1,1,1,1,1, 1,0,0,0,0, 1,1,1,1,0, 1,0,0,0,0, 1,1,1,1,1),
    'F'=>   array(1,1,1,1,1, 1,0,0,0,0, 1,1,1,1,0, 1,0,0,0,0, 1,0,0,0,0),
    'G'=>   array(1,1,1,1,1, 1,0,0,0,0, 1,0,0,1,1, 1,0,0,0,1, 1,1,1,1,1),
    'H'=>   array(1,0,0,0,1, 1,0,0,0,1, 1,1,1,1,1, 1,0,0,0,1, 1,0,0,0,1),
    'I'=>   array(1,1,1,1,1, 0,0,1,0,0, 0,0,1,0,0, 0,0,1,0,0, 1,1,1,1,1),
    'J'=>   array(0,0,0,1,1, 0,0,0,0,1, 0,0,0,0,1, 1,0,0,0,1, 1,1,1,1,1),
    'K'=>   array(1,0,0,0,1, 1,0,0,1,0, 1,1,1,0,0, 1,0,0,1,0, 1,0,0,0,1),
    'L'=>   array(1,0,0,0,0, 1,0,0,0,0, 1,0,0,0,0, 1,0,0,0,0, 1,1,1,1,1),
    'M'=>   array(1,0,0,0,1, 1,1,0,1,1, 1,0,1,0,1, 1,0,0,0,1, 1,0,0,0,1),
    'N'=>   array(1,0,0,0,1, 1,1,0,0,1, 1,0,1,0,1, 1,0,0,1,1, 1,0,0,0,1),
    'O'=>   array(0,1,1,1,0, 1,0,0,0,1, 1,0,0,0,1, 1,0,0,0,1, 0,1,1,1,0),
    'P'=>   array(1,1,1,1,0, 1,0,0,0,1, 1,1,1,1,0, 1,0,0,0,0, 1,0,0,0,0),
    'Q'=>   array(1,1,1,1,1, 1,0,0,0,1, 1,0,0,0,1, 1,1,1,1,1, 0,0,1,0,0),
    'R'=>   array(1,1,1,1,0, 1,0,0,0,1, 1,1,1,1,0, 1,0,0,0,1, 1,0,0,0,1),
    'S'=>   array(1,1,1,1,1, 1,0,0,0,0, 1,1,1,1,1, 0,0,0,0,1, 1,1,1,1,1),
    'T'=>   array(1,1,1,1,1, 0,0,1,0,0, 0,0,1,0,0, 0,0,1,0,0, 0,0,1,0,0),
    'U'=>   array(1,0,0,0,1, 1,0,0,0,1, 1,0,0,0,1, 1,0,0,0,1, 1,1,1,1,1),
    'V'=>   array(1,0,0,0,1, 1,0,0,0,1, 0,1,0,1,0, 0,1,0,1,0, 0,0,1,0,0),
    'W'=>   array(1,0,0,0,1, 1,0,0,0,1, 1,0,1,0,1, 1,0,1,0,1, 0,1,0,1,0),
    'X'=>   array(1,0,0,0,1, 0,1,0,1,0, 0,0,1,0,0, 0,1,0,1,0, 1,0,0,0,1),
    'Y'=>   array(1,0,0,0,1, 1,0,0,0,1, 0,1,0,1,0, 0,0,1,0,0, 0,0,1,0,0),
    'Z'=>   array(1,1,1,1,1, 0,0,0,1,0, 0,0,1,0,0, 0,1,0,0,0, 1,1,1,1,1),
    '!'=>   array(0,0,1,0,0, 0,0,1,0,0, 0,0,1,0,0, 0,0,0,0,0, 0,0,1,0,0),
    ' '=>   array(0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0),
);

My function allows the styling of dots too e.g. colour. use a monospaced font like Courier or the dots could be offset compared to the nbsp.

Here's an example...

505eb416465cee0d768d47dd3ae1a35d

My function parameters take 'classnames' for fill and void - so if a void classname is passed, it creates dots instead of nbsp for the voids.

Also you could vary the shapes, e.g. '♦' for a diamond instead of dots - it looks sick though ;)

So enclose each letter in an inline-block div

I had an hour drive home so, so the inline element came to mind, should have thought about that before I asked...

BTW - it's probably easier to read the binary than to convert to integer - that was just me playing ;)

thanks, i was wondering about that.

very neat and fun. thanks for the tips....

Thanks, that was interesting to work on!

7d27ce747088bf1b2ab1a35a575e8497

Member Avatar for diafol

he he. It was fun

Hey guys!
Spoke to my lecturer today. He expected us to use a whole lot of If else statements and echo statements.to do this. I argued that we'd have to have so many if else statements and time consuming! Below is a sample of what he expected.

<?php
$initials = "MV";
for $i=0;$i<strlen($initials);$i=$i+1
{
if ($initials{$i}=="M")
{
echo "**&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp**&nbsp**<br>";
echo "**&nbsp**&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp**&nbsp&nbsp&nbsp**<br>";
echo "**&nbsp&nbsp**&nbsp&nbsp&nbsp&nbsp**&nbsp&nbsp&nbsp&nbsp**<br>";
echo "**&nbsp&nbsp&nbsp**&nbsp&nbsp**&nbsp&nbsp&nbsp&nbsp&nbsp**<br>";
echo "**&nbsp&nbsp&nbsp&nbsp**&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp**<br>";
echo "**&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp**<br>";
echo "**&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp**<br>";
echo "**&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp**<br>";

}
}
?>

Not the complete code,but just to give you an idea as to what was expected.
If I may ask,I'm curious to know how diafol got those binary combinations for each letter?

Thanks guys

You are correct. There are several ways to implement a solution like this. Using if...else statements for each letter and then echo'ing the results in each block is time consuming to build, but simple to build without having to know a lot about programming concepts such as arrays or functions.

I dont know the details of diafol's implementation, but i can tell you based on the information he provided above, i used arrays to store the "pixel" information (0 = no pixel, 1 = pixel). In your example, there is no array, just echo'ing at each block. You have (&nbsp; for 0 and ** for 1).

Well, in my case, i took the input and split it up into characters, storing each character within an array, then read through the array of input, identified each character in the array, then found the array that stored the binary (on/off) information for that character, then built the HTML while looping through the remainder of characters from the original input.

In your example, you are doing the same thing except you arent storing the data in any type of array. In your example, if you decide to use a different character for the "on" pixel, you have to update every line in your code. The beauty behind storing 0 and 1 values in an array, if you want to use a different character to display, you change it only in one place in your code.

how diafol got those binary combinations for each letter?

He most likely got a grid of 5x5 and drew out the characters in the grid. Where he wanted an "on" pixel, he represents that with 1. Where there is an "off" pixel, that is represented with 0.

in my case, my implementation was done in asp.net/c#, but that doesnt matter. The concept here would be the same regardless of server side scripting language.

commented: yep, pretty much it +14
Member Avatar for diafol

OK see my post above with the alphabet array. e.g.

'A'=>   array(1,1,1,1,1, 1,0,0,0,1, 1,0,0,0,1, 1,1,1,1,1, 1,0,0,0,1),

This is arranged into a 5x5 grid - I think it's clear that the above will give...

*****
*   *
*   *
*****
*   *

Equally you could have done this:

'A'=>   array(0,0,1,0,0, 0,1,0,1,0, 1,0,0,0,1, 1,1,1,1,1, 1,0,0,0,1),

  *  
 * *  
*   *
*****
*   *

To get the binary string you just implode the item...

A = '0010001010100011111110001'

So if you want, you can store these instead...

$alphabet = array(
    'A'=>   '1111110001100011111110001',
    'B'=>   '1111110001111101000111111',
    'C'=>   '1111110000100001000011111',

Of course you could have the html output instead...

$alphabet = array(
    'A'=>   '&bull;&bull;&bull;&bull;&bull;<br />&bull;&nbsp;&nbsp;&nbsp;&bull;<br />&bull;&nbsp;&nbsp;&nbsp;&bull;<br />&bull;&bull;&bull;&bull;&bull;<br />&bull;&nbsp;&nbsp;&nbsp;&bull;',
    'B'=>   '&bull;&bull;&bull;&bull;&bull;<br />v&bull;&nbsp;&nbsp;&nbsp;&bull;<br />&bull;&bull;&bull;&bull;&nbsp;<br />&bull;&nbsp;&nbsp;&nbsp;&bull;<br />&bull;&bull;&bull;&bull;&bull;',
    'C'=>   '&bull;&bull;&bull;&bull;&bull;<br />&bull;&nbsp;&nbsp;&nbsp;&nbsp;<br />&bull;&nbsp;&nbsp;&nbsp;&nbsp;<br />&bull;&nbsp;&nbsp;&nbsp;&nbsp;<br />&bull;&bull;&bull;&bull;&bull;',

A bit more awkward and less flexible if you want to vary the output, but still, if that's all that's required fine.

HOWEVER - I wouldn't do any IF statements - there's no need.

Get your text and split it:

$chars = str_split($word);
$outputChars = array();
$sep = '...'; //whatever you want this to be
foreach($chars as $char)
{
    $outputChars[] = "<div class='char'>{$alphabet[$char]}</div>"; 
}
$output = implode("<div class="sep">$sep</div>", $outputChars);
echo $output;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.