Function to summarize string in php

Asked

Viewed 791 times

1

Well I set up the following function to summarize a string in php.

function resumo($string,$chars) {
if (strlen($string) > $chars) {
    while (substr($string, $chars, 1) <> ' ' && ($chars < strlen($string))) {
        $chars++;
    }
}
return substr($string, 0, $chars);
}

echo resumo("bla bla bla bla bla", 4);

Well the problem I am having is the following. I inform the amount of at most 4 letters and the function is returning me more letters.

And whenever Sting is summarized I want to add (...) three points. If I report a string that does not need to be summarized it is not necessary to add the three dots.

  • I don’t understand why you need that while, would not only return the substr() ?

  • This makes things much easier https://answall.com/a/169912/3635

2 answers

3


function resumo($string, $chars) {
    if (strlen($string) > $chars)
        return substr($string, 0, $chars).'...';
    else
        return $string;
}

echo resumo("abcde", 4);

Exit

abcd...

You can also use the function mb_strimwidth, you can use it this way:

function resumo($string, $chars) {
    return mb_strimwidth($string, 0, $chars + 3, "...");
}

According to the response of @Wallace

  • 1

    Curious that the short output was larger than the xD kkk input

  • kkkk well this, to avoid this could add the variable char with 2. I would avoid this problem

  • In fact, his response was flagged as low quality in relation to size/content. I recommend that in addition to the code you put some explanation about it.

  • @Robertofagundes was right, thank you.

  • 1

    @Hugoborges this will not work well with Unicode, still recommend testing mb_strimwidth, as in Wallace’s answer: https://answall.com/a/169912/3635

  • @Guilhermenascimento the problem is, how will I make the stitches appear only if I have to abbreviate? There’s a way you can put an answer.

  • 1

    @Hugoborges exactly what the function does, if it passes the limit it adds the three points, otherwise it does not. There is no need to post a new answer and it is no longer possible, because the question is already marked as dup and Wallace’s answer solves the problem, if you want to vote up on Wallace’s answer to thank him about the collaboration with the tip. ;)

  • I changed the answer by adding an example

  • Thank you, it’s been fixed

  • 1

    @Robertofagundes edited, I also recommend that you adjust the limitation code by making the character account, but ae is optional.

  • I don’t know what you mean

  • @Robertofagundes did not understand what I said about "edited" or how to use the function?

  • I did not understand this comment: recomendo ainda que ajuste o código de limitação fazendo a conta de caracteres, mas ae é opcional..

  • @Guilhermenascimento thus became the function. function resumo($string, $chars) {&#xA; &#xA; // Calcula os pontos&#xA; $chars += 3;&#xA; &#xA; // Retorno&#xA; return mb_strimwidth($string, 0, $chars, "...");&#xA;}

  • @Hugoborges does not need, your code is correct, is that I had not understood right, but for what I tested this ok ;)

Show 10 more comments

2

You can simplify your role with substr() or mb_substr() how to return and change the function signature to add the ... if no argument is passed:

function resumo($string, $chars=0) {
    if(!mb_strlen($string)) return '';
    return  $chars == 0 ? $string : substr($string, 0, $chars)  .'...';
}

echo resumo("bla bla bla bla bla", 4); //Output = "bla ..."
echo resumo("bla bla bla bla bla");    //Output = "bla bla bla bla bla"
echo resumo("bl", 5);                  //Output = "bl..."

See working on repl it..

  • that’s right, thank you.

  • There is only one detail, the (...) can only appear if Sting is summarized.

Browser other questions tagged

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