Count vowels of a string in PHP7

4

I have the following statement: I have to count the total characters of a string, count the total number of vowels, and the number of times each vowel appears. I lose myself a bit and I've done it without a loop while. I can not use arrays either. The mistake I get when I want to count vowels separately is:

Warning: substr() expects parameter 2 to be integer, string given in /opt/lampp/htdocs/pruebaexamen/ejercicio1.php on line 5

Warning: substr() expects parameter 2 to be integer, string given in /opt/lampp/htdocs/pruebaexamen/ejercicio1.php on line 6

Warning: substr() expects parameter 2 to be integer, string given in /opt/lampp/htdocs/pruebaexamen/ejercicio1.php on line 7

Warning: substr() expects parameter 2 to be integer, string given in /opt/lampp/htdocs/pruebaexamen/ejercicio1.php on line 8

Warning: substr() expects parameter 2 to be integer, string given in /opt/lampp/htdocs/pruebaexamen/ejercicio1.php on line 9

The PHP code:

<?php
    $texto = $_POST['texto'];
        $caracteres = strlen($texto);
        $vocales = substr_count($texto, 'a')+substr_count($texto, 'e')+substr_count($texto, 'i')+substr_count($texto, 'o')+substr_count($texto, 'u')+substr_count($texto, 'A')+substr_count($texto, 'E')+substr_count($texto, 'I')+substr_count($texto, 'O')+substr_count($texto, 'U');
        $a = substr_count($texto, 'a') + substr($texto, 'A');
        $e = substr_count($texto, 'e') + substr($texto, 'E');
        $i = substr_count($texto, 'i') + substr($texto, 'I');
        $o = substr_count($texto, 'o') + substr($texto, 'O');
        $u = substr_count($texto, 'u') + substr($texto, 'U');
        echo "El número total de caracteres es $caracteres <br/>";
        echo "El total de vocales es $vocales <br/>";
        echo "El numero de letras a (mayúsculas incluidas) es $a <br/>";
        echo "El numero de letras e (mayúsculas incluidas) es $e <br/>";
        echo "El numero de letras i (mayúsculas incluidas) es $i <br/>";
        echo "El numero de letras o (mayúsculas incluidas) es $o <br/>";
        echo "El numero de letras u (mayúsculas incluidas) es $u <br/>";
?>
    
asked by ras212 04.11.2016 в 21:19
source

3 answers

3

Your code but "correct". You were wrong putting substr , not substr_count

PHP:

<?php
    $texto = $_POST['texto'];
        $caracteres = strlen($texto);
        $vocales = substr_count($texto, 'a')+substr_count($texto, 'e')+substr_count($texto, 'i')+substr_count($texto, 'o')+substr_count($texto, 'u')+substr_count($texto, 'A')+substr_count($texto, 'E')+substr_count($texto, 'I')+substr_count($texto, 'O')+substr_count($texto, 'U');
        $a = substr_count($texto, 'a') + substr_count($texto, 'A');
        $e = substr_count($texto, 'e') + substr_count($texto, 'E');
        $i = substr_count($texto, 'i') + substr_count($texto, 'I');
        $o = substr_count($texto, 'o') + substr_count($texto, 'O');
        $u = substr_count($texto, 'u') + substr_count($texto, 'U');
        echo "El número total de caracteres es $caracteres <br/>";
        echo "El total de vocales es $vocales <br/>";
        echo "El numero de letras a (mayúsculas incluidas) es $a <br/>";
        echo "El numero de letras e (mayúsculas incluidas) es $e <br/>";
        echo "El numero de letras i (mayúsculas incluidas) es $i <br/>";
        echo "El numero de letras o (mayúsculas incluidas) es $o <br/>";
        echo "El numero de letras u (mayúsculas incluidas) es $u <br/>";
?>
    
answered by 04.11.2016 / 22:07
source
0

To avoid complicating yourself with so much code, I recommend you do a regex:

return preg_match_all('/[aeiou]/i',$str,$matches);
    
answered by 29.09.2018 в 00:26
-1

EDIT: Your code works fine because you're using substr_count for lowercase letters but you're trying to make only substr for uppercase. That's why it gives you WARNING, because the substr method needs two integer parameters and not a String. You will also have to remove the <\br> tags.

Your corrected code would be:

<?php
    $texto = "oapresoiresoihriohesoirejsroijes";
    $caracteres = strlen($texto);
    $vocales = substr_count($texto, 'a')+substr_count($texto, 'e')+substr_count($texto, 'i')+substr_count($texto, 'o')+substr_count($texto, 'u')+substr_count($texto, 'A')+substr_count($texto, 'E')+substr_count($texto, 'I')+substr_count($texto, 'O')+substr_count($texto, 'U');
    $a = substr_count($texto, 'a') + substr_count($texto, 'A');
    $e = substr_count($texto, 'e') + substr_count($texto, 'E');
    $i = substr_count($texto, 'i') + substr_count($texto, 'I');
    $o = substr_count($texto, 'o') + substr_count($texto, 'O');
    $u = substr_count($texto, 'u') + substr_count($texto, 'U');
    echo "El número total de caracteres es $caracteres \n";
    echo "El total de vocales es $vocales";
    echo "El numero de letras a (mayúsculas incluidas) es $a \n";
    echo "El numero de letras e (mayúsculas incluidas) es $e \n>";
    echo "El numero de letras i (mayúsculas incluidas) es $i \n";
    echo "El numero de letras o (mayúsculas incluidas) es $o \n";
    echo "El numero de letras u (mayúsculas incluidas) es $u \n";
?>

Original response : I had not read that a requirement was not to use arrays. In case they can be used, I'll leave the example in case it could be useful to someone in the future.

You do not need to do any while loop.

With PHP's own functions you can do the following:

<?php
      $cadena = "oapresoiresoihriohesoirejsroijes";

      //Divides la cadena por carácteres y lo almacenas en un array.
      $array = str_split($cadena); 

      //Estableces cuales son los valores que te quieres quedar
      $vocales = array('a', 'e', 'i', 'o', 'u'); 

      //Con array_filter quitas los valores que no quieres en tu array.
      //O mejor dicho, te quedas sólo con los que necesitas.
      $arrayQuitandoConsonantes = array_filter($array, function($val) use ($vocales) {
          return in_array($val, $vocales);
      });

      //Muestras la cantidad de veces que está contenida cada vocal
      print_r(array_count_values($arrayQuitandoConsonantes)); 

For this chain, the corresponding output will be:

Array
(
    [o] => 6
    [a] => 1
    [e] => 5
    [i] => 5
)
    
answered by 04.11.2016 в 21:31