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

Truncate check length of append #33

Merged
merged 2 commits into from
Oct 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions spec/Coduo/PHPHumanizer/StringSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,17 @@ function it_truncate_string_to_word_closest_to_a_certain_number_of_characters()
$this->truncate($text, 0)->shouldReturn("Lorem");
$this->truncate($text, 0, '...')->shouldReturn("Lorem...");
$this->truncate($text, -2)->shouldReturn($text);

$textShort = 'Short text';
$this->truncate($textShort, 1, '...')->shouldReturn("Short...");
$this->truncate($textShort, 2, '...')->shouldReturn("Short...");
$this->truncate($textShort, 3, '...')->shouldReturn("Short...");
$this->truncate($textShort, 4, '...')->shouldReturn("Short...");
$this->truncate($textShort, 5, '...')->shouldReturn("Short...");
$this->truncate($textShort, 6, '...')->shouldReturn("Short...");
$this->truncate($textShort, 7, '...')->shouldReturn("Short text");
$this->truncate($textShort, 8, '...')->shouldReturn("Short text");
$this->truncate($textShort, 9, '...')->shouldReturn("Short text");
$this->truncate($textShort, 10, '...')->shouldReturn("Short text");
}
}
2 changes: 1 addition & 1 deletion src/Coduo/PHPHumanizer/String/Truncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct($text, $charactersCount, $append = '')

public function __toString()
{
if ($this->charactersCount < 0 || strlen($this->text) <= $this->charactersCount) {
if ($this->charactersCount < 0 || strlen($this->text) <= ($this->charactersCount + mb_strlen($this->append))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should switch to mb_strlen everywhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just find out that we have more places like that, I gonna merge it now and prepare separated PR for switching to mb_* functions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a good idea, i've had quite a bit of problems with multibyte string being french where we have stange characters everywhere ! You should add some test in strange charset like arabic/persian (علی) (see: http://stackoverflow.com/questions/25599981/how-can-i-use-strlen-in-php-for-persian)

return $this->text;
}

Expand Down