katarra Posted October 10, 2009 Share Posted October 10, 2009 I am writing a paragraph that will be an echo out of a php script. However, the variable can either have a beginning letter that is a vowel or not. Is there a way to program in that if the variable starts with a vowel to make the script produce the "an" option and if it starts with a consonant to produce the "a" option? Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/ Share on other sites More sharing options...
Garethp Posted October 10, 2009 Share Posted October 10, 2009 What do you mean by "an" option and "a" option? Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934307 Share on other sites More sharing options...
.josh Posted October 10, 2009 Share Posted October 10, 2009 I have no idea what this "a" and "an" "option" you speak of is, but you can do something like this: $string = "blah"; $vowels = array('a','e','i','o','u'); if (in_array(substr($string,0,1),$vowels)) { // string starts with a vowel, do something } else { // string starts with a consonant, do something } Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934308 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 For instance, the $class can either be: Assassin or Hunter. I have an echo coming out that: Adam appears to be a $class. Well, if that class is Assassin, then it prints out: Adam appears to be a assassin. To be grammatically correct, it should be "an assassin" not "a assassin" But, if I were to make the echo as: Adam appears to be an $class. That prints out: Adam appears to be an hunter. (in this case). Again, it's not grammatically correct, as it should be "a hunter" not "an hunter". Is there anything I can do/write to make the php decide if there should be "a" or "an" in front of the $class to make it grammatically correct? Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934312 Share on other sites More sharing options...
Daniel0 Posted October 10, 2009 Share Posted October 10, 2009 I have no idea what this "a" and "an" "option" you speak of is, but you can do something like this: $string = "blah"; $vowels = array('a','e','i','o','u'); if (in_array(substr($string,0,1),$vowels)) { // string starts with a vowel, do something } else { // string starts with a consonant, do something } That's not entirely fool-proof. The rule is that you put the indefinite article 'an' before a vowel sound, not just before vowels. It's called "an hour", but "a horse" and it's called "an Xbox", but "a xylophone". Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934315 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 True. I was being simplistic. Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934318 Share on other sites More sharing options...
.josh Posted October 10, 2009 Share Posted October 10, 2009 true, but from the context of his last post, I somehow doubt $class will == "hour". I suppose you could alternatively do this: $array = array('a' => 'hunter', 'an' => 'assassin'); or $array['a'] = array('hunter','barbarian',etc...); $array['an'] = array('assassin', etc...); Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934321 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 Okay, that would seem to work (the array bit). Now, a really newb question (and yeah, I am... *hangs head), how do I program that in to my code to make it choose? I copied the 2nd array option into my coding. How do I get it to work? Here's my code so far: <?=$HeShe?> has been trained as <?=$class?> Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934329 Share on other sites More sharing options...
.josh Posted October 10, 2009 Share Posted October 10, 2009 function getAdj($class) { $array['a'] = array('hunter','barbarian'); $array['an'] = array('assassin','animal'); foreach($array as $k => $v) if (in_array(strtolower($class),$v)) return $k; return ''; } echo "Adam appears to be " . getAdj($class) . " " . $class; Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934345 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 Hmm... that didn't quite work... Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934354 Share on other sites More sharing options...
.josh Posted October 10, 2009 Share Posted October 10, 2009 Well I'm sorry to say I am not psychic so you'll have to tell me what didn't quite work. When I do this: function getAdj($class) { $array['a'] = array('hunter','barbarian'); $array['an'] = array('assassin','animal'); foreach($array as $k => $v) if (in_array(strtolower($class),$v)) return $k; return ''; } $a = array('hunter','animal','barbarian','assassin'); foreach ($a as $class) { echo "Adam appears to be " . getAdj($class) . " " . $class . "<br/>"; } I get this: Adam appears to be a hunter Adam appears to be an animal Adam appears to be a barbarian Adam appears to be an assassin Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934358 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 Alright, when I added in the $a = array('hunter','animal','barbarian','assassin'); line it worked when I just copied/pasted the whole bit into another portion of my script. How do I mesh it into my coding below? Okay, here is the coding that I have right now. I don't have the echo bit, as this is in a section of a form. You can see that I put my coding in the <? ?> portions so I need to adjust the coding to fit within those parameters. <?=$HeShe?> has been trained as <?=$class?>. Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934364 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 Here is the full coding, just so you can see what I'm trying to do. You can see where I'm trying to put this in down near the bottom past the php coding. <? $command = 1; $peep = ucwords(strtolower($action3[2])); $peepsname = getuser($peep,"name"); $peeps = getlevel(get("plane"),coords(),"pcs"); $pflags = getuser($peep,"flags"); function getAdj($class) { $array['a'] = array('hunter','Royal'); $array['an'] = array('assassin','entertainer'); foreach($array as $k => $v) if (in_array(strtolower($class),$v)) return $k; return ''; } $a = array('hunter','animal','barbarian','assassin'); foreach ($a as $class) { echo "Adam appears to be " . getAdj($class) . " " . $class . "<br/>"; } if ($peep != $username && (strpos($peeps,"$peep:") === false || strpos(getuser($peep,"flags"),"(invis)") !== false)) echo("You do not see $peep here."); else{ $name = "<i>".getuser($peep,"name")."</i>"; $race = "<i>".getuser($peep,"race")."</i>"; $gender = getuser($peep,"gender"); if ($gender == "male"){ $HeShe = "He"; $heshe = "he"; $hisher = "his"; }else if ($gender == "female"){ $HeShe = "She"; $heshe = "she"; $hisher = "her"; }else{ $HeShe = "It"; $heshe = "it"; $hisher = "its"; } if (getuser($peep,"guild") == "") $guild = "$name is not a member of a guild."; else $guild = "$name is a member of the guild <i>".getuser($peep,"guild")."</i>."; $gender = "<i>$gender</i>"; $home = "<i>".getuser($peep,"hometown")."</i>"; $eyes = "<i>".getuser($peep,"eyes")."</i>"; $hair = "<i>".getuser($peep,"hair")."</i>"; $skin = "<i>".getuser($peep,"skin")."</i>"; $height = "<i>".getuser($peep,"height")."</I>"; $build = "<i>".getuser($peep,"build")."</i>"; $class = "<i>".getuser($peep,"class")."</i>"; $wep = getuser($peep,"weapon"); if($wep)$wep="$name is wielding a <i>$wep</i>.";else $wep="$name has <i>no weapon</i>."; $ret = ""; if ($peep == $username){ $petdata = getuser($peep,"pet"); $ret = "The ".get("race")." known as ".get("name")." is here"; if ($petdata[1] != "") $ret .= " with ".get("pet"); $ret .= ".<br><br>\n"; } $other = ""; $spouse = getuser($peep,"spouse"); if ($spouse == "none") $spouse = "not married."; else $spouse = "married to <i>".getuser($spouse,"name")."</i>. "; $shield="<i>".getuser($peep,"shield")."</i>";if($shield=="<i></i>")$shield="<i>nothing</i>"; $head="<i>".getuser($peep,"head")."</i>";if($head=="<i></i>")$head="<i>nothing</i>"; $neck="<i>".getuser($peep,"neck")."</i>";if($neck=="<i></i>")$neck="<i>nothing</i>"; $body="<i>".getuser($peep,"body")."</i>";if($body=="<i></i>")$body="<i>nothing</i>"; $leftarm="<i>".getuser($peep,"leftarm")."</i>";if($leftarm=="<i></i>")$leftarm="<i>nothing</i>"; $rightarm="<i>".getuser($peep,"rightarm")."</i>";if($rightarm=="<i></i>")$rightarm="<i>nothing</i>"; $wrists="<i>".getuser($peep,"wrists")."</i>";if($wrists=="<i></i>")$wrists="<i>nothing</i>"; $hands="<i>".getuser($peep,"hands")."</i>";if($hands=="<i></i>")$hands="<i>nothing</i>"; $leftfinger="<i>".getuser($peep,"leftfinger")."</i>";if($leftfinger=="<i></i>")$leftfinger="<i>nothing</i>"; $rightfinger="<i>".getuser($peep,"rightfinger")."</i>";if($rightfinger=="<i></i>")$rightfinger="<i>nothing</i>"; $legs="<i>".getuser($peep,"legs")."</i>";if($legs=="<i></i>")$legs="<i>nothing</i>"; $feet="<i>".getuser($peep,"feet")."</i>";if($feet=="<i></i>")$feet="<i>nothing</i>"; $arm="$HeShe has $shield equipped for a shield, $head on $hisher head, $neck around $hisher neck and $body on $hisher body. $HeShe is also wearing $leftarm on $hisher left arm and a $rightarm on $hisher right arm. $HeShe has equipped $wrists on $hisher wrists, $hands on $hisher hands, $leftfinger on $hisher left finger and a $rightfinger on $hisher right finger. $HeShe has $legs on $hisher legs and $feet on $hisher feet. $HeShe is $spouse"; if(strpos(getuser($peep,"flags"),"(equip)")===false)$arm=""; if ($peep != $username){ $myname = get("name"); set("chat",get("chat")."<b><font class=self>You looked at $peepsname.</font></b><br>\n"); $chardata = explode(":",getlevel(get("plane"),coords(),"pcs")); $numchars = count($chardata); for ($i = 0; $i < $numchars; $i++){ if ($chardata[$i] != $username && $chardata[$i] != ""){ $peepname = $chardata[$i]; $cp = getuser($peepname,"chatpref"); if ($chardata[$i] == $peep){$peepchat = "<b>$myname looked at you.</b><br>\n"; if ($cp[10] == 1) setuser($peepname,"newchat",1);} else if(strpos(getuser($peepname,"friends"),"$username:") !== false) $peepchat = "<font class=friends>$myname looked at $peepsname.</font><br>\n"; else $peepchat = "$myname looked at $peepsname.<br>\n"; setuser($peepname,"chat",getuser($peepname,"chat").$peepchat); if ($chardata[$i] != $peep && $cp[11] == 1) setuser($peepname,"newchat",1); } } } } ?> <table width="85%" border="0" cellpadding="10" align="center"> <tr> <td><h2 align="center"><?=$name?></h2></td> </tr> <tr> <td> <div align="left"> <?=$name?> is a member of the <?=$race?>. <?=$HeShe?> has been trained as <?=$class?>. </div> <p align="left"><?=$wep?> <?=$arm?></p></td> </tr> <tr> <td> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934366 Share on other sites More sharing options...
.josh Posted October 10, 2009 Share Posted October 10, 2009 <?=$HeShe?> has been trained as <?=$class?>. well you added the function to your script. You have to actually call the function in that line there. Look at the example of the output I posted. the whole $a = array... and foreach was an example of using the function. Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934372 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 Okay, I understand, but for some reason, my $class variables are capitalized and that array doesn't seem to recognize the variables if they are capitalized. Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934376 Share on other sites More sharing options...
.josh Posted October 10, 2009 Share Posted October 10, 2009 that's not your problem. the function uses strtolower to lowercase what you give it. Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934379 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 Here is the code I have in now: <?=$HeShe?> has been trained as <?=getAdj($class) . " " . $class?>. But, it doesn't print out the a or an bit... Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934380 Share on other sites More sharing options...
.josh Posted October 10, 2009 Share Posted October 10, 2009 is the specific class in one of the arrays in the function? the function i posted has hunter, barbarian, assassin and animal, as examples. you need to populate those arrays with the classes you actually have. Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934382 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 Yeah, I added 'alchemist' to it, which is the class this is being called to. Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934383 Share on other sites More sharing options...
.josh Posted October 10, 2009 Share Posted October 10, 2009 are you sure it's spelled the same as what's in $class? post your code as it currently looks. Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934384 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 Ok, I got it to print out in the paragraph portion whether it is capitalized or not. But now, it's not choosing between the "a" or "an" bit. Here is the code I have now. I deleted the (strtolower($class),$v) bit and it works in that regard, but yeah, it doesn't delineate between a or an. function getAdj($class) { $array['a'] = array('hunter','Royal'); $array['an'] = array('assassin','entertainer','alchemist'); foreach($array as $k => $v) if (in_array) return $k; return ''; } $a = array('hunter','entertainer','Royal','assassin','alchemist'); Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934387 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 Here is the coding in the paragraph: <?=$HeShe?> has been trained as <?=getAdj($class) . " " . $class?>. Here is what it prints out: He has been trained as alchemist. It prints out just fine when I add an echo after the function, but down in the paragraph section, it isn't. So, it has something to do with how it is presented in the coding of the paragraph (top code). Code in the function: function getAdj($class) { $array['a'] = array('hunter','Royal'); $array['an'] = array('assassin','entertainer','alchemist'); foreach($array as $k => $v) if (in_array(($class),$v)) return $k; return ''; } $a = array('hunter','entertainer','Royal','assassin','alchemist'); Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934390 Share on other sites More sharing options...
Alex Posted October 10, 2009 Share Posted October 10, 2009 Why don't you just use the rules of English to get the a or an? eg: function getAdj($word) { $vowels = Array('a', 'e', 'i', 'o', 'u'); return in_array(strtolower($word[0]), $vowels) ? 'an' : 'a'; } Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934392 Share on other sites More sharing options...
.josh Posted October 10, 2009 Share Posted October 10, 2009 Why don't you just use the rules of English to get the a or an? eg: function getAdj($word) { $vowels = Array('a', 'e', 'i', 'o', 'u'); return in_array(strtolower($word[0]), $vowels) ? 'an' : 'a'; } We already went over that. Why don't you just read before you post? katarra...omg dude you're making this more difficult than it needs to be. There was nothing wrong with the function I gave you, except for you needing to add the classes to the arrays. That's all you need to change in it. Stop messing with it! Also, you don't need that $a it was just an example of how to use the function. Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934393 Share on other sites More sharing options...
katarra Posted October 10, 2009 Author Share Posted October 10, 2009 I could, but I still need to know how to put it into my coding so that it produces the a or an in the string. (described above in the paragraph coding) Quote Link to comment https://forums.phpfreaks.com/topic/177200-solved-code-to-help-choose-a-or-an/#findComment-934395 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.