stewarty77 Posted March 24, 2006 Share Posted March 24, 2006 HiSay if the user was to input a string "hello". I need php to print the string by letter and give its numerical position in the alphabet:h = 8e = 5l = 12l = 12o = 15Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/5689-splitting-a-string-then-showing-its-alphabetical-position/ Share on other sites More sharing options...
keeB Posted March 24, 2006 Share Posted March 24, 2006 Write a function to do it. It wouldn't take long..[code]function returnPosInAlphabet($char) { switch ($char) { case 'a': return 1; case 'b': return 2; }}[/code][= Quote Link to comment https://forums.phpfreaks.com/topic/5689-splitting-a-string-then-showing-its-alphabetical-position/#findComment-20310 Share on other sites More sharing options...
stewarty77 Posted March 24, 2006 Author Share Posted March 24, 2006 [!--quoteo(post=357975:date=Mar 24 2006, 05:12 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 24 2006, 05:12 PM) [snapback]357975[/snapback][/div][div class=\'quotemain\'][!--quotec--]Write a function to do it. It wouldn't take long..[code]function returnPosInAlphabet($char) { switch ($char) { case 'a': return 1; case 'b': return 2; }}[/code][=[/quote]Ok got the function createdfunction returnPosInAlphabet($char) { switch ($char) { case 'A': return 1; case 'B': return 2; case 'C': return 3; case 'D': return 4; case 'E': return 5; case 'F': return 6; case 'G': return 7; case 'H': return 8; case 'I': return 9; case 'J': return 10; case 'K': return 11; case 'L': return 12; case 'M': return 13; case 'N': return 14; case 'O': return 15; case 'P': return 16; case 'Q': return 17; case 'R': return 18; case 'S': return 19; case 'T': return 20; case 'U': return 21; case 'V': return 22; case 'W': return 23; case 'X': return 24; case 'Y': return 25; case 'Z': return 26; }}I'm using str_split("string entered") to split the string entered but it returns Array, anymore suggestions what to do next, oh and I'm not to good at the programming as you might have guessed! Quote Link to comment https://forums.phpfreaks.com/topic/5689-splitting-a-string-then-showing-its-alphabetical-position/#findComment-20330 Share on other sites More sharing options...
keeB Posted March 24, 2006 Share Posted March 24, 2006 [code]<?phpfunction returnPosInAlphabet($char) { switch ($char) { case 'A': return 1; case 'B': return 2; case 'C': return 3; case 'D': return 4; case 'E': return 5; case 'F': return 6; case 'G': return 7; case 'H': return 8; case 'I': return 9; case 'J': return 10; case 'K': return 11; case 'L': return 12; case 'M': return 13; case 'N': return 14; case 'O': return 15; case 'P': return 16; case 'Q': return 17; case 'R': return 18; case 'S': return 19; case 'T': return 20; case 'U': return 21; case 'V': return 22; case 'W': return 23; case 'X': return 24; case 'Y': return 25; case 'Z': return 26; default: return "space"; }}$str = "Hello Friend";$hi = str_split($str); for ($x = 0; $x <= count($hi); $x++) { print "' $hi[$x] ' = "; print returnPosInAlphabet(strtoupper($hi[$x])); print "<br>"; }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5689-splitting-a-string-then-showing-its-alphabetical-position/#findComment-20336 Share on other sites More sharing options...
Barand Posted March 24, 2006 Share Posted March 24, 2006 Obviously from the "Why use 1 line of code when 50 will do" school of programming - do you charge per line of code ;-);[code]function returnPosInAlphabet($char) { return ord(strtoupper($char))-64;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5689-splitting-a-string-then-showing-its-alphabetical-position/#findComment-20391 Share on other sites More sharing options...
kenrbnsn Posted March 24, 2006 Share Posted March 24, 2006 I have been trying to post a similar solution for the past 2 hours but keep getting a "406 error -- Not Acceptible" error. :-(Ken(since this post worked, let's see if editing the post to add code will work)(nope :-( ) Quote Link to comment https://forums.phpfreaks.com/topic/5689-splitting-a-string-then-showing-its-alphabetical-position/#findComment-20393 Share on other sites More sharing options...
stewarty77 Posted March 26, 2006 Author Share Posted March 26, 2006 [!--quoteo(post=358059:date=Mar 24 2006, 10:23 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 24 2006, 10:23 PM) [snapback]358059[/snapback][/div][div class=\'quotemain\'][!--quotec--]I have been trying to post a similar solution for the past 2 hours but keep getting a "406 error -- Not Acceptible" error. :-(Ken(since this post worked, let's see if editing the post to add code will work)(nope :-( )[/quote]Thanks your help with this matter is greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/5689-splitting-a-string-then-showing-its-alphabetical-position/#findComment-20888 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.