Jump to content

[SOLVED] Code to help choose "a" or "an"


katarra

Recommended Posts

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?

 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?>.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

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';
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.