How to invert words and count total characters

Asked

Viewed 1,047 times

3

As I do via php/html, for example, I have a input for user to type anything, and the time he clicks on the "OK" button, I return the word he typed in reverse, and the amount of characters of the word he typed in?

I know I’ll have to use the methods strlen() and strrev(), but I can’t do it. Can someone help me? Grateful

  • 1

    Julio, welcome to only. Your question was not within the standards of the site, so it was edited. If you do not agree, edit it, however, improving it. Visit How to create a good question to know how to ask a good question.

  • Will not use any of these xD functions by amazing.

4 answers

7

If Encode (ISO-8859-1) is configured correctly the functions strlen() and strrev() work as expected, if using UTF-8 prefer the approach below.

stlen() Does not count the number of characters but the byte, a practical example is input ação returns 6(bytes) and not 4(characters), for multibite encoding characters use mb_strlen().

strrev() suffers from the same problem, does not treat multibyte characters so does not do the inversion correctly, in which case use regex to solve the job.

Example with strlen() & strrev()

$str = 'AÇÃO';
printf("%s - %s caracteres - invertido: %s", $str, strlen($str), strrev($str));

Satida:

AÇÃO - 6 caracteres - invertido: O�Ç�A

Example with regex & mb_strlen()

function mb_strrev($str){
    preg_match_all('/./us', $str, $ar);
    return implode('', array_reverse($ar[0]));
}

$str = 'AÇÃO';
printf("%s - %s caracteres - invertido: %s", $str, strlen($str), strrev($str));

Satida:

AÇÃO - 4 caracteres - invertido: OÃÇA
  • Nice abortion on the Charsets! Interesting detail!

6

I suggest something like this:

 $input = $_GET['nome_do_campo'];
 $invertido = strrev($input);
 $tamanho = strlen($input);

echo "String invertida: <em>{$invertido}</em>. O tamanho é de <strong>{$tamanho}</strong> caracteres";

5

Simple

<?php

    $string = (isset($_POST['input1'])) ? trim($_POST['input1']) : null;

    $stringInvertida = strrev($string);
    $qntCaracteres = strlen($string);

    echo "Seu texto invertido é: {$stringInvertida} e contem {$qntCaracteres} caractere(s).";

In your HTML form, set the name do input that will receive the string as input1

  • Thank you beast!!

  • @Julia, if it is not complicated for you, choose the answer of rray, is more complete and effective.

  • So brother, for some reason, you’re only answering me 0

  • Follows the codes

  • @Juliecase, check the properties of your form if you have method="post" and the input name is correct.

1

A multibyte character support version:

$str = '日本語'; // teste essa palavra alienígena
$str = 'Ação'; // ou será que essa palavra é alienígena?

echo 'quantidade de letras: '.mb_strlen($str);
echo PHP_EOL.'<br />inverso: '.join('', array_reverse(preg_split('~~u', $str, -1, PREG_SPLIT_NO_EMPTY)));

The native function strrev() is not "multibyte safe", so the implementation using join(), array_reverse() and preg_split().

The function mb_strlen() is multibyte character specific and returns the number of characters. Do not confuse with number of bytes.

If you want to know the amount of bytes, use strlen().

See the difference

$str = 'Ação';
echo strlen($str).PHP_EOL.'<br />';
echo mb_strlen($str).PHP_EOL;
  • Good example +1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.