Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter feature request (text limit / word wrap) #3

Open
mklemarczyk opened this issue May 17, 2015 · 10 comments
Open

Formatter feature request (text limit / word wrap) #3

mklemarczyk opened this issue May 17, 2015 · 10 comments
Labels
status:ready for adoption Feel free to implement this issue. type:enhancement Enhancement

Comments

@mklemarczyk
Copy link

I do not have concept to implement this thing, but I think that it can be extension to asText formatter method.

First, there are a few methods of word wrapping:

  • Strict (as much as possible characters, can break the word)
  • Word rounded up (include last word that reach the limit)
  • Word rounded down (include last word before reach the limit)

I want use word wrapping to limit text.
I know that I should use substr, but this method will break last word.
There is also wordwrap, but first it does have third wrapping method, and secondly it only insert break line character.

This feature can be used in:

  • DataGrid columns and DetailView to display first few words of content,
  • Headers on page and page title, when it use some field that can be too long. (For example "question content" on "question update page",
@mklemarczyk
Copy link
Author

Thanks @lynicidn, but none method form this class cover my case.
I used truncate for now, but it break last word.

@lynicidn
Copy link

show how you use it

@mklemarczyk
Copy link
Author

Example:

$this->title = Yii::t('app', 'Update: {question}', [
    'question' => StringHelper::truncate($model->question, 20)
]);

In question is string: Do you like drink coffee at work?
Function return string: Update: Do you like drink co...
I expect string: Update: Do you like drink...

@mklemarczyk
Copy link
Author

@lynicidn I do not know how many words should be printed. It depends on word lengths.

@lynicidn
Copy link

@mklemarczyk ok, you right - i don't help for you - sorry.

simple way use truncate + find "white space of end" and apply endWith ('white space + partword + ...')

@taxp
Copy link

taxp commented May 18, 2015

Here is what you need. strrpos with negative limit finds last space in string before the limit.

    function truncate($string, $limit)
    {
        return substr($string, 0, strrpos($string, ' ', $limit - strlen($string)));
    }

    echo truncate('Do you like drink coffee at work?', 20);

Output is 'Do you like drink'

@SilverFire
Copy link
Member

StringHelper::truncateWords() is available since Yii 2.0.1

@cebe
Copy link
Member

cebe commented Aug 16, 2017

as far as I understood the request it was to truncate words based on a maximum string length. StringHelper::truncateWords() is based on word count.

@samdark samdark reopened this Aug 16, 2017
@samdark samdark transferred this issue from yiisoft/yii2 Apr 18, 2019
@samdark samdark added status:ready for adoption Feel free to implement this issue. type:enhancement Enhancement labels Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:enhancement Enhancement
Projects
None yet
Development

No branches or pull requests

6 participants