# Thread: convert this to a PHP script?

1. ## convert this to a PHP script?

a member of my forum recently posted this:
Ever wonder about those people who say they are giving more than 100%?

We have all been to those meetings where someone wants over 100%.

How about achieving 103%? Here's a little math that might prove helpful.

What makes life 100%?

If

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z is represented

as:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26.

Then,

H A R D W O R K
8 1 18 4 23 15 18 11 = 98%

K N O W L E D G E
11 14 15 23 12 5 4 7 5 = 96%

But,

A T T I T U D E
1 20 20 9 20 21 4 5 = 100%

And,

B U L L S H I T
2 21 12 12 19 8 9 20 = 103%

So, it stands to reason that hardwork and knowledge will get you close, attitude will get you there, but bull**** will put you over the top.

And look how far

A S S K I S S I N G
1 19 19 11 9 19 19 9 14 7 = 118%

will take you.
im still learning PHP, and i took it upon myself to try and convert the above into a PHP script.
so if someone enters the word: PUNCTUALITY
the script outputs: 16 21 14 3 20 21 1 12 9 20 25
(P=16, U=21, N=14 ... etc.)
but im having trouble.

i dont know HOW to make my script ADD up the value of each letter.
know whatta mean?

so when someone enters a word, it shows them the numerical % equvilant.
here is what i have so far:
Code:
```<?php
//= Fri. Feb. 4, 2005 @ 9:24 PM
//=>> This creates a 2 row table. Row #1 = A-Z; Row #2 = 1-26.
\$TDwidth = 100/26 . "%"; // make widths of each cell equal.
print "<table border='0' cellpadding='2' cellspacing='2' width='100%' align='center' style='font-family: arial; font-size: 12px'><tr style=\"background-color: #ccc; \">";

//= Row #1 = A-Z
\$letter = "A";
\$i = "0";
while (\$i++ < 26) {
print "<td width=\$TDwidth align=center>" . \$letter++ . "</td>";
}

print "</tr><tr style=\"background-color: #ccc; \">";

//= Row #2 = 1-26
\$number = "1";
\$ii = "0";
while (\$ii++ < 26) {
print "<td align=center>" . \$number++ . "</td>";
}
print "</tr></table><hr>";
//=<< END TABLE

//=>> Enter word to be converted to numerical values.
\$originalphrase = "A S S K I S S I N G";
echo "<u>Original</u>: " . \$originalphrase . "<br />"; //= Print out word.

\$stripped_phrase = ereg_replace("[^A-Za-z]", "", \$originalphrase);
echo "<u>Stripped of non-alphanumeric characters</u>: " . \$stripped_phrase . "<br />"; //= Print out word; stripped.

\$lowered_phrase = strtolower(\$stripped_phrase);
echo "<u>Stripped of non-alphanumeric characters & lowercased</u>: " . \$lowered_phrase . "<hr>"; //= Print out word; stripped/lowercased.

\$letters = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
\$numbers = array("1 ","2 ","3 ","4 ","5 ","6 ","7 ","8 ","9 ","10 ","11 ","12 ","13 ","14 ","15 ","16 ","17 ","18 ","19 ","20 ","21 ","22 ","23 ","24 ","25 ","26");

\$newphrase = str_replace(\$letters, \$numbers, \$lowered_phrase); //= Replace each letter with its numerical value.

echo \$originalphrase . " = " . \$newphrase . "<p>";

//=>> Print the sum of all values in coverted word.
\$array = array("\$newphrase");
//As of PHP4.0.4 the function array_sum() exists, it's used as such :
\$sum   = array_sum(\$array); // 26
echo "Congratulations!!<br />Your hard efforts of <b>" . \$lowered_phrase . "</b> = <b>" . \$sum . " %</b>";
//=<< END

?>```
the last few lines is where i am really lost.
i dont know how to add them up.

the way i currently have it, only shows the value of the first letter.

nor do i know how to make my script with a TextBox, so they can enter their own word, and press Submit, to find out the results of their efforts.

any and all assistance would be greatly appreciated.
and no need to write a whole script, unless my methods are wrong or incorrect.

2. This should be fun, I haven't the ability right now to do this but I will later.

so long

3. Originally Posted by Ap0s7le
This should be fun, I haven't the ability right now to do this but I will later.

so long
thanks amigo.

4. Here ya go. That was fun.

PHP Code:
``` <?php //= Fri. Feb. 4, 2005 @ 9:24 PM /***************************************** * *    Changed 12:20am Sat Feb 5th 2005 *    by Ap0s7le * ******************************************/ //=>> This creates a 2 row table. Row #1 = A-Z; Row #2 = 1-26.     \$TDwidth = 100/26 . "%"; // make widths of each cell equal.     print "<table border='0' cellpadding='2' cellspacing='2' width='100%' align='center' style='font-family: arial; font-size: 12px'><tr style=\"background-color: #ccc; \">";     //= Row #1 = A-Z           \$letter = "A";           \$i = "0";           while (\$i++ < 26) {               print "<td width=\$TDwidth align=center>" . \$letter++ . "</td>";           }     print "</tr><tr style=\"background-color: #ccc; \">";     //= Row #2 = 1-26           \$number = "1";           \$ii = "0";           while (\$ii++ < 26) {               print "<td align=center>" . \$number++ . "</td>";           }     print "</tr></table><hr>"; //=<< END TABLE ?> Add your word. <form method="post" action="<?=\$_SERVER['PHP_SELF'];?>"> <input type="text" name="original" /> <input type="submit" name="submit" value="Click it, I dare you." /> </form> <? //=>> Enter word to be converted to numerical values. \$originalphrase = (isset(\$_POST['original'])) ? \$_POST['original'] : "Asskissing";     echo "<u>Original</u>: " . \$originalphrase . "<br />"; //= Print out word.     \$stripped_phrase = ereg_replace("[^A-Za-z]", "", \$originalphrase);     echo "<u>Stripped of non-alphanumeric characters</u>: " . \$stripped_phrase . "<br />"; //= Print out word; stripped.     \$lowered_phrase = strtolower(\$stripped_phrase);     echo "<u>Stripped of non-alphanumeric characters & lowercased</u>: " . \$lowered_phrase . "<hr>"; //= Print out word; stripped/lowercased.     \$letters = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");     \$numbers = array("1 ","2 ","3 ","4 ","5 ","6 ","7 ","8 ","9 ","10 ","11 ","12 ","13 ","14 ","15 ","16 ","17 ","18 ","19 ","20 ","21 ","22 ","23 ","24 ","25 ","26");     \$newphrase = str_replace(\$letters, \$numbers, \$lowered_phrase); //= Replace each letter with its numerical value.     echo \$originalphrase . " = " . \$newphrase . "<p>"; //=>> Print the sum of all values in coverted word.                  \$d = explode(" ", \$newphrase);                  \$sum = NULL;         foreach (\$d AS \$value)         {             \$sum = \$sum+\$value;         }         echo "Congratulations!!<br />Your hard efforts of <b>" . \$lowered_phrase . "</b> = <b>" . \$sum . " %</b>"; //=<< END ?> ```

5. Ap0s7le beat me to it.
but umm, i think my code is slightly better. in that it doesn't require the user to have spaces in their words for a correct calculation to be achieved. and it's much much shorter code.

\$originalphrase="H A R D W O R K";
\$originalphrase=str_replace(" ","",\$originalphrase);
\$letters=range("a","z");
\$numbers=range("1","26");

\$loweredphrase=trim(strtolower(\$originalphrase));
\$newphrase=preg_split("//",\$loweredphrase,-1,PREG_SPLIT_NO_EMPTY);
\$newphrase=str_replace(\$letters,\$numbers,\$newphrase);
\$sum=array_sum(\$newphrase);
echo \$sum;

OH, and by the way, ASSKISSING=127!

6. Yup, yours is nicer. I thought I'd use what he'd written rather than having to do anything else.

so long

7. thanks to everyone that helped me tonight.

your responses were quicker than i anticipated.

8. final product:

hope you all enjoy.
LOL

thanks again everyone for your help.

<< i know it was not necessary, but i provided proper credit to Ap0s7le for helping me out >>

9. Thank you Jammo, my name in lights! Yes!
I'd like to thank my Mom, for always being there........
hehe, seriously though, I do appreciate the credit.

later

10. DOH!
Originally Posted by Ap0s7le
Thank you Jammo, my name in lights! Yes!
I'd like to thank my Mom, for always being there........
hehe, seriously though, I do appreciate the credit.

later
ya forgot to thank your High School Teachers?!?!
lol.

no problem buddy.
ya helped. i give credit.
thats how it shold be, right?

11. lol, the word I say too much of while coding gives me 143%
YEAH!

nice script hahaha

Your efforts of m****f*cker = 143 %.
You should stop kissing ***! SERIOUSLY!!!

12. Congratulations!!
Your efforts of zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = 988 %. You should stop kissing ***! SERIOUSLY!!!
haha

13. Originally Posted by whofarted
haha
Haha... I did that, too. Only I did it up to 358,688 (something around 350k).

D O N O T H I N G = 106%
That's pretty much accurate.

14. LOL @ Everyone... I can't say what I was putting in there, my wife wouldn't be happy!!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•