RE: [PHP] Help with array / list looping syntax
Robb Kerr wrote: > I need some help with a looping syntax. In english, "a" is used before > words that begin with consonants - "an" is used before words that > start with vowels. You are probably already aware of this, but that isn't strictly correct. The rule for deciding between "a" and "an" is based not on the presence of consonants and vowels, but on the presence of consonant and vowel _sounds_. Most of the time these are the same, but sometimes they are not, such as in the phrases "an honorable agreement" or "a useful idea". This distinction may not matter for your program, but I thought I'd mention it just in case you hadn't considered it. > I'm trying to create a loop that checks this state > and inserts the correct word in the echo line. Below is the sloppy > version of what I'm trying to do... [...] > There's got to a cleaner way to do this. Here's my version: if (preg_match('/^[aeiou]/i', $word)) { echo 'an'; } else { echo 'a'; } Mine is probably the least efficient approach of all those given thus far from a pure performance standpoint, since I'm incurring the overhead of the regex engine for something that can be accomplished without it, but IMHO it is so much easier on the eyes that it's worth it. Of course others may disagree. :) HTH -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help with array / list looping syntax
On Wed, 30 Jun 2004 00:58:32 + (GMT), Philip Olson wrote: >>> I need some help with a looping syntax. In english, "a" is used before >>> words that begin with consonants - "an" is used before words that start >>> with vowels. I'm trying to create a loop that checks this state and inserts >>> the correct word in the echo line. Below is the sloppy version of what I'm >>> trying to do... >>> >>> >> if (substr($table['field'], 0, 1)=="a") OR (substr($table['field'], 0, >>> 1)=="e") OR (substr($table['field'], 0, 1)=="i") OR >>> (substr($table['field'], 0, 1)=="u") OR (substr($table['field'], 0, >>> 1)=="y") } >>> echo 'Member has an ' . $table['field'] . ' who is...'; >>> } else { >>> echo 'Member has a ' . $table['field'] . 'who is...'; >>> } >>> ?> >>> >>> There's got to a cleaner way to do this. Perhaps using a list or array >>> containing the vowels and steping through it checking the first letter of >>> the field contents against each member of the list. But, I don't know how >>> to code this system. Please help. >>> >> >> How about this? >> >> $vowels = array('a', 'e', 'i', 'o', 'u'); >> echo 'Member has a'.(in_array($table['field'][0], $vowels) ? 'n' : >> '').' '.$table['field'].' who is...'; > > Yet another solution (just for fun): > > $vowels = 'aeiou'; > if (false !== strpos($vowels,strtolower($table['field']{0}))) echo 'n'; > > Regards, > Philip Thank you for your suggestions. I know it's not as elegant as your suggestions, but I settled on the following because it's easy for me to read and understand when I view the code later. I also listed both upper and lower case vowels in the array because according to the PHP Manual, in_array() is case sensitive. Thanx again, Robb -- Robb Kerr Digital IGUANA -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help with array / list looping syntax
> > > I need some help with a looping syntax. In english, "a" is used before > > > words that begin with consonants - "an" is used before words that start > > > with vowels. I'm trying to create a loop that checks this state and inserts > > > the correct word in the echo line. Below is the sloppy version of what I'm > > > trying to do... One more variant: function a_an ($word) { return (stristr ("aeiouAEIOU", $word{0}) ? "an " : "a ") . $word; } echo "Member has " . a_an ($table['field']) . " who is..."; Any time you start typing minor variants of the same thing over and over, an alarm should go off in your head. That's what computers are here to save us from! pb -- paul bissex, e-scribe.com -- database-driven web development 413.585.8095 69.55.225.29 01061-0847 72°39'71"W 42°19'42"N -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help with array / list looping syntax
> > I need some help with a looping syntax. In english, "a" is used before > > words that begin with consonants - "an" is used before words that start > > with vowels. I'm trying to create a loop that checks this state and inserts > > the correct word in the echo line. Below is the sloppy version of what I'm > > trying to do... > > > > > if (substr($table['field'], 0, 1)=="a") OR (substr($table['field'], 0, > > 1)=="e") OR (substr($table['field'], 0, 1)=="i") OR > > (substr($table['field'], 0, 1)=="u") OR (substr($table['field'], 0, > > 1)=="y") } > > echo 'Member has an ' . $table['field'] . ' who is...'; > > } else { > > echo 'Member has a ' . $table['field'] . 'who is...'; > > } > > ?> > > > > There's got to a cleaner way to do this. Perhaps using a list or array > > containing the vowels and steping through it checking the first letter of > > the field contents against each member of the list. But, I don't know how > > to code this system. Please help. > > > > How about this? > > $vowels = array('a', 'e', 'i', 'o', 'u'); > echo 'Member has a'.(in_array($table['field'][0], $vowels) ? 'n' : > '').' '.$table['field'].' who is...'; Yet another solution (just for fun): $vowels = 'aeiou'; if (false !== strpos($vowels,strtolower($table['field']{0}))) echo 'n'; Regards, Philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help with array / list looping syntax
On Tue, 29 Jun 2004 19:27:04 -0500, Robb Kerr <[EMAIL PROTECTED]> wrote: > > I need some help with a looping syntax. In english, "a" is used before > words that begin with consonants - "an" is used before words that start > with vowels. I'm trying to create a loop that checks this state and inserts > the correct word in the echo line. Below is the sloppy version of what I'm > trying to do... > > if (substr($table['field'], 0, 1)=="a") OR (substr($table['field'], 0, > 1)=="e") OR (substr($table['field'], 0, 1)=="i") OR > (substr($table['field'], 0, 1)=="u") OR (substr($table['field'], 0, > 1)=="y") } > echo 'Member has an ' . $table['field'] . ' who is...'; > } else { > echo 'Member has a ' . $table['field'] . 'who is...'; > } > ?> > > There's got to a cleaner way to do this. Perhaps using a list or array > containing the vowels and steping through it checking the first letter of > the field contents against each member of the list. But, I don't know how > to code this system. Please help. > How about this? $vowels = array('a', 'e', 'i', 'o', 'u'); echo 'Member has a'.(in_array($table['field'][0], $vowels) ? 'n' : '').' '.$table['field'].' who is...'; -- paperCrane --Justin Patrin-- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Help with array / list looping syntax
I need some help with a looping syntax. In english, "a" is used before words that begin with consonants - "an" is used before words that start with vowels. I'm trying to create a loop that checks this state and inserts the correct word in the echo line. Below is the sloppy version of what I'm trying to do... There's got to a cleaner way to do this. Perhaps using a list or array containing the vowels and steping through it checking the first letter of the field contents against each member of the list. But, I don't know how to code this system. Please help. Thanx, Robb -- Robb Kerr Digital IGUANA Helping Digital Artists Achieve their Dreams http://www.digitaliguana.com http://www.cancerreallysucks.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php