Check function only returns empty

Asked

Viewed 21 times

-2

I made these two functions very simple to limit the characters and to see if the column in the empty database, for some reason only returns empty even having results.

public function limite_texto($texto, $limite, $complemento) {
    return mb_strimwidth(utf8_encode($texto), 0, $limite, $complemento);
}

public function vazio($texto, $limite, $complemento, $aviso) {
    if (empty($texto)) :
        return $aviso;
    else :
        return $this->limite_texto($texto, $limite, $complemento);
    endif;
}

$ctlr->vazio("oooooi", 3, "...", "O campo está vazio")

For some reason ta returning only the "...", when the expected would be "Ooo..." or if the variable $texto were empty to return O campo está vazio, I think I made a mistake in logic

1 answer

3


You defined that the function should return 3 characters, you got 3 characters, but you found it strange because they didn’t come 6? Strange.

Perhaps more attention was lacking when reading the documentation of the functions you are using:

mb_strimwidth

Value returned: The truncated string. If trimmarker is set, trimmarker Replaces the last chars to match the width.

Is returned to string truncated, but if the value of trimmarker is defined, trimmarker will replace the last characters of string until the limit size is reached.

I mean, you get to string "oooooi", truncated "ooo", but how did you define the value of trimmarker, it will overwrite the last characters up to a maximum of 3 characters (limit you set).

So the way out will always be "...", because only the value of trimmarker already reaches the character limit you set.

If the goal was to get "ooo...", you should have set the limit to 6, since you want to have 6 characters in the output.

  • The intention was to make this function add the "3 dots" limiting the number of characters only of the variable $texto

Browser other questions tagged

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