Help - Search - Members - Calendar
Full Version: How Do I Check If A Word Starts With A Vowel?
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
Runickitty
Is it possible to check whether a word begins with a vowel and/or ends with s?
swordz
I hate regex, so I give no guarantee.

preg_match('/^[aeiou]|s\z/i', $string)

Come back if it doesn't work, someone else may have replied with the correct answer by then.

swordz
Aeonsky
This should do it...

CODE
<?php

$a = "ITeFvaello";
$f = strtolower($a[0]);

function checkifvowel($v) {
if ($v == "a" || $v == "e" || $v == "i" || $v == "o" || $v == "u")
echo "First letter is a vowel!"; else echo "It is not a vowel!";
}

checkifvowel($f);

?>
Trice
QUOTE(swordz @ Sep 17 2008, 09:49 PM) *
I hate regex, so I give no guarantee.

preg_match('/^[aeiou]|s\z/i', $string)

Come back if it doesn't work, someone else may have replied with the correct answer by then.

swordz


I tested it swordz, and it does seem to work, how i used it:

CODE
<?php

$yourstring = "ITeFvaello";
$loweredstring = strtolower($yourstring[0]);

function checkifvowel($string) {

  if(preg_match('/^[aeiou]|s\z/i', $string))
  {
    echo 'match';
  }else{
    echo 'no match';
  }

}

checkifvowel($loweredstring);

?>



QUOTE(Aeonsky @ Oct 15 2008, 10:37 PM) *
This should do it...

CODE
<?php

$a = "ITeFvaello";
$f = strtolower($a[0]);

function checkifvowel($v) {
if ($v == "a" || $v == "e" || $v == "i" || $v == "o" || $v == "u")
echo "First letter is a vowel!"; else echo "It is not a vowel!";
}

checkifvowel($f);

?>


I think in my opinion it's a bit poor programming and looks tacky having multiple || in one statement. If you were forced to use it i would suggest you use an array. There are two ways that this can be done. Could be with a loop as i have shown below:

CODE
<?php

$yourstring = "ITeFvaello";
$loweredstring = strtolower($yourstring[0]);

function checkifvowel($string) {

$vowel =array('a','e','i','o','u');

$i=0;
while($i < count($vowel)) {
  
    if($string==$vowel[$i])
    {
    echo 'First letter is a vowel!';
    }
    $i++;
  
}

}

checkifvowel($loweredstring);

?>



Or you could just use a function called in_array(). This does pretty much what the above does but with less lines of code, (ie eliminating us implementing a while loop, an example of this i have written and shown below:

CODE
<?php

$yourstring = "ITeFvaello";
$loweredstring = strtolower($yourstring[0]);

function checkifvowel($string) {

  $vowel =array('a','e','i','o','u');

  if(in_array($string, $vowel))
  {
    echo 'Word begins with a vowel';
  }

}

checkifvowel($loweredstring);

?>
simrn
I ususally do this with in_array(). and it works just fine ... it's easy to manipulate
IamShipon1988
That's exactly what he did simrn. At first he defined what $vowel was using array listing and then just simply opened an if statement with in_array. I like swordz method a lot better as it is very simple and does not require you to do as much work.
simrn
ahan ... but I never used that method smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.