PHP script to find first vowel in a name

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 327
  • Loc: Canada

Post 3+ Months Ago

Hi All

I can't seem to be able to create a php script that will find the first vowel in a name.

Take as example, JOSEPH

We see the first vowel is O

I want the script to check each letter of the name and when it finds the first vowel, to stop and echo it.

Sounds simple but can't seem to do it.

Would appreciate some help

Thanks
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

found an idea, but need to expand a little more on it.
Code: [ Select ]
<?php
$name = 'joseph';
$findme  = 'o';
$pos = strpos($name, $findme);

// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
  echo "The string '$findme' was not found in the string '$name'";
} else {
  echo "The string '$findme' was found in the string '$name'";
  echo " and exists at position $pos";
}
?>
  1. <?php
  2. $name = 'joseph';
  3. $findme  = 'o';
  4. $pos = strpos($name, $findme);
  5. // Note our use of ===. Simply == would not work as expected
  6. // because the position of 'a' was the 0th (first) character.
  7. if ($pos === false) {
  8.   echo "The string '$findme' was not found in the string '$name'";
  9. } else {
  10.   echo "The string '$findme' was found in the string '$name'";
  11.   echo " and exists at position $pos";
  12. }
  13. ?>

last attempt lol
Code: [ Select ]
<?php
$name = ('joesph');
$check = array(1 => '$a','$e','$i','$o','$u');
$a  = "a";
$e  = "e";
$i  = "i";
$o  = "o";
$u  = "u";
$pos = strpos($name, $check);

// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
  echo "'$check' was not found in the string '$name'";
} else {
  echo "The string '$check' was found in the string '$name'";
  echo " and exists at position $pos";
}
?>
  1. <?php
  2. $name = ('joesph');
  3. $check = array(1 => '$a','$e','$i','$o','$u');
  4. $a  = "a";
  5. $e  = "e";
  6. $i  = "i";
  7. $o  = "o";
  8. $u  = "u";
  9. $pos = strpos($name, $check);
  10. // Note our use of ===. Simply == would not work as expected
  11. // because the position of 'a' was the 0th (first) character.
  12. if ($pos === false) {
  13.   echo "'$check' was not found in the string '$name'";
  14. } else {
  15.   echo "The string '$check' was found in the string '$name'";
  16.   echo " and exists at position $pos";
  17. }
  18. ?>
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

PHP Code: [ Select ]
<?
function first_vowel($name) {
    $vowels = array('a', 'e', 'i', 'o', 'u');
    $length = strlen($name);
    for ($i = 0; $i < $length; ++$i) {
        if (in_array($vowels, $name[$i])) {
            return $name[$i];
        }
    }
    return '';
}
?>
  1. <?
  2. function first_vowel($name) {
  3.     $vowels = array('a', 'e', 'i', 'o', 'u');
  4.     $length = strlen($name);
  5.     for ($i = 0; $i < $length; ++$i) {
  6.         if (in_array($vowels, $name[$i])) {
  7.             return $name[$i];
  8.         }
  9.     }
  10.     return '';
  11. }
  12. ?>


Probably want to do some additional validation on the input but you get the idea.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 327
  • Loc: Canada

Post 3+ Months Ago

Hi spork

Found samll error in code

changed:
from: $i = 0,
to: $i = 0;

Functions always give me a bit of trouble.

I am having trouble putting in a name and echoing the output.
Could you change your code so that I can enter a name variable and then echo the output vowel.

thanks
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

Would you not action POST the data.

name.php has form then action POST takename.php to produce results.

I assume....
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 327
  • Loc: Canada

Post 3+ Months Ago

I have a form on a page

The user enters his name and then submits it.

I want to transfer the submitted name to the function and echo it
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

Code: [ Select ]
<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>

</body>
</html>
  1. <html>
  2. <body>
  3. <form action="welcome.php" method="post">
  4. Name: <input type="text" name="fname">
  5. Age: <input type="text" name="age">
  6. <input type="submit">
  7. </form>
  8. </body>
  9. </html>

Code: [ Select ]
<html>
<body>

Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html>
  1. <html>
  2. <body>
  3. Welcome <?php echo $_POST["fname"]; ?>!<br>
  4. You are <?php echo $_POST["age"]; ?> years old.
  5. </body>
  6. </html>


found this sample in w3schools, should be a start. if your still having issues with it then i will give it a go later on.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 327
  • Loc: Canada

Post 3+ Months Ago

My problem is not setting up a form but getting the name to the function

For example:

Code: [ Select ]
$input = "joseph";
$firstvowel = first_vowel($input);
echo $firstvowel;
  1. $input = "joseph";
  2. $firstvowel = first_vowel($input);
  3. echo $firstvowel;

When I try the above, I keep getting errors about array

This is my problem, do not know how to incorporate input into function
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 327
  • Loc: Canada

Post 3+ Months Ago

I managed to figure it out.

There was another error in the function which I changed:

From:if (in_array($vowels, $name[$i]))
To: if (in_array($name[$i], $vowels))

I was getting array errors, because the second parameter was not an array.

Now things work.

The following is the final code I got to work;

Code: [ Select ]
<?php
function first_vowel($name)
{
  $vowels = array('A', 'E', 'I', 'O', 'U');
  $length = strlen($name);
  for ($i = 0; $i < $length; ++$i)
  {
    if (in_array($name[$i], $vowels))
    {
      return $name[$i];
    }
  }
  return '';
}
$input ="JOSEPH";
$firstvowel = first_vowel($input);
echo "The First Vowel is : ".$firstvowel;
?>
  1. <?php
  2. function first_vowel($name)
  3. {
  4.   $vowels = array('A', 'E', 'I', 'O', 'U');
  5.   $length = strlen($name);
  6.   for ($i = 0; $i < $length; ++$i)
  7.   {
  8.     if (in_array($name[$i], $vowels))
  9.     {
  10.       return $name[$i];
  11.     }
  12.   }
  13.   return '';
  14. }
  15. $input ="JOSEPH";
  16. $firstvowel = first_vowel($input);
  17. echo "The First Vowel is : ".$firstvowel;
  18. ?>
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

Sorry about the typos; I don't have PHP installed anywhere so I didn't verify it before posting.

This isn't homework, is it?
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 327
  • Loc: Canada

Post 3+ Months Ago

Hi Spork

Your typos acually did me a favour.
Like they say, you learn more by your mistakes.

If you hadn't given me the first step to work on, I would not have come up with the solution.

Thanks for your help

Post Information

  • Total Posts in this topic: 11 posts
  • Users browsing this forum: No registered users and 55 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.