Script PHP para encontrar la primera vocal en un nombre

  • wpas
  • Graduate
  • Graduate
  • Avatar de Usuario
  • Registrado: Jul 12, 2010
  • Mensajes: 214
  • Loc: Canada
  • Status: Offline

Nota Noviembre 29th, 2012, 11:30 pm

Hola a todos

¡ Aparentemente no puedo crear un script php que encontrará la primera vocal en un nombre.

Tomar como ejemplo, JOSEPH

Vemos que la primera vocal es o

Quiero el script para comprobar cada letra del nombre y cuando encuentre la primera vocal, para detener y lo echo.

Sonidos parece simple pero no puede hacerlo.

Agradeceria alguna ayuda

Gracias
http://www.schembrionics.com
The Ultimate Solutions Center
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Noviembre 29th, 2012, 11:30 pm

  • Zealous
  • Guru
  • Guru
  • Avatar de Usuario
  • Registrado: Abr 15, 2011
  • Mensajes: 1195
  • Loc: Sydney
  • Status: Offline

Nota Noviembre 30th, 2012, 4:28 am

encontrar una idea, pero la necesidad de ampliar un poco más en él.
Código: [ 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. ?>

último intento jejeje
Código: [ 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
  • Avatar de Usuario
  • Registrado: Sep 22, 2003
  • Mensajes: 6130
  • Loc: Seattle, WA
  • Status: Offline

Nota Noviembre 30th, 2012, 5:07 pm

PHP Código: [ 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. ?>


Probablemente quiere hacer algunos validación adicional en la entrada, pero usted consigue la idea.
The Beer Monocle. Classy.
  • wpas
  • Graduate
  • Graduate
  • Avatar de Usuario
  • Registrado: Jul 12, 2010
  • Mensajes: 214
  • Loc: Canada
  • Status: Offline

Nota Diciembre 1st, 2012, 12:25 pm

Hola spork

Encontrado samll error en el código

cambiado:
desde: $i = 0,
a: $i = 0;

Las funciones siempre me dan un poco de problemas.

Tengo problemas para poner un nombre y haciéndose eco de la salida.
¿Se puede cambiar su código para que pueda ingresar una variable de nombre y then echo la vocal de la salida.

Gracias
http://www.schembrionics.com
The Ultimate Solutions Center
  • Zealous
  • Guru
  • Guru
  • Avatar de Usuario
  • Registrado: Abr 15, 2011
  • Mensajes: 1195
  • Loc: Sydney
  • Status: Offline

Nota Diciembre 1st, 2012, 7:30 pm

¿Acción no registrar los datos.

Name.php tiene forma entonces acción POST takename.php para producir resultados.

Supongo....
  • wpas
  • Graduate
  • Graduate
  • Avatar de Usuario
  • Registrado: Jul 12, 2010
  • Mensajes: 214
  • Loc: Canada
  • Status: Offline

Nota Diciembre 1st, 2012, 10:04 pm

Tengo un formulario en una página

El usuario escribe su nombre y, a continuación, envía.

Quiero transferir el nombre enviado a la función y lo echo
http://www.schembrionics.com
The Ultimate Solutions Center
  • Zealous
  • Guru
  • Guru
  • Avatar de Usuario
  • Registrado: Abr 15, 2011
  • Mensajes: 1195
  • Loc: Sydney
  • Status: Offline

Nota Diciembre 1st, 2012, 10:36 pm

Código: [ 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>

Código: [ 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>


encontrar este ejemplo en w3schools, debe ser un comienzo. Si tu sigue teniendo problemas con ella entonces daré es un ir más tarde en.
  • wpas
  • Graduate
  • Graduate
  • Avatar de Usuario
  • Registrado: Jul 12, 2010
  • Mensajes: 214
  • Loc: Canada
  • Status: Offline

Nota Diciembre 2nd, 2012, 1:39 am

Mi problema no es configurar un formulario pero obteniendo el nombre de la función

Por ejemplo:

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

Cuando trato de lo anterior, sigo recibiendo errores de matriz

Este es mi problema, no sé cómo incorporar la entrada en función
http://www.schembrionics.com
The Ultimate Solutions Center
  • wpas
  • Graduate
  • Graduate
  • Avatar de Usuario
  • Registrado: Jul 12, 2010
  • Mensajes: 214
  • Loc: Canada
  • Status: Offline

Nota Diciembre 2nd, 2012, 2:01 am

Logré entenderlo.

Hubo otro error en la función que he cambiado:

De: Si (in_array (vocales$, $name[$i]))
Para: Si (in_array ($ nombre [$ i], vocales $))

Me estaba poniendo errores de matriz, porque el segundo parámetro no era una matriz.

Ahora las cosas funcionan.

El siguiente es el código final que llegué a trabajar;

Código: [ 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. ?>
http://www.schembrionics.com
The Ultimate Solutions Center
  • spork
  • Brewmaster
  • Silver Member
  • Avatar de Usuario
  • Registrado: Sep 22, 2003
  • Mensajes: 6130
  • Loc: Seattle, WA
  • Status: Offline

Nota Diciembre 6th, 2012, 3:40 pm

Perdón por los errores tipográficos; No tengo PHP instalado en cualquier lugar, por lo que no comprobarlo antes de publicar.

Esto no es tarea, ¿verdad?
The Beer Monocle. Classy.
  • wpas
  • Graduate
  • Graduate
  • Avatar de Usuario
  • Registrado: Jul 12, 2010
  • Mensajes: 214
  • Loc: Canada
  • Status: Offline

Nota Diciembre 6th, 2012, 5:30 pm

Hola Spork

Su acually de errores tipográficos me hicieron un favor.
Como dicen, usted aprender más por sus errores.

Si no me había dado el primer paso para trabajar, yo no habría surgido con la solución.

Gracias por tu ayuda
http://www.schembrionics.com
The Ultimate Solutions Center

Publicar Información

  • Total de mensajes en este tema: 11 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 207 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC