php-htmltruncator icon indicating copy to clipboard operation
php-htmltruncator copied to clipboard

Returning nothing (just "...") on certain input strings, but only at certain truncation lengths

Open hi2u opened this issue 8 years ago • 0 comments

I've got some autogenerated text in my database generated by https://github.com/fzaninotto/Faker

Some of the strings it generated seem to break php-htmltruncator. I noticed that these strings contain a bit of stuff like "* * * * * * * * * * *", so I'm wondering if this could be related to the cause.

Here's some code to demonstrate this...

$originalStrings[] = '* * * Gryphon. \'Then, you know,\' Alice gently remarked; \'they\'d  have been was not much like keeping so close to her, still it was growing, and very soon finished it off. * * * * * * * * * * * * * * * * * * * * * * * * * * * CHAPTER II. The Pool of Tears \'Curiouser and curiouser!\' cried Alice in a ring, and begged the Mouse was speaking, and this was the same thing, you know.\' \'And what are YOUR shoes done with?\' said the King, \'and don\'t look at the other side. The further off from England the nearer is to France-- Then turn not pale, beloved snail, but come and join the dance. Will you, won\'t you, will you, won\'t you, will you join the dance? Will you, won\'t you, will you join the dance. Will you, won\'t you, will you, won\'t you, will you, won\'t you join the dance. Would not, could not, would not, could not, would not, could not, would not give all else for two reasons. First, because I\'m on the same side of WHAT?\' thought Alice; \'I might as well as if she did not like to go down the little door into that lovely garden. First, however, she went round the court with a little now and then, if I might venture to ask any more questions about it, you may stand down,\' continued the Hatter, who turned pale and fidgeted. \'Give your evidence,\' the King put on one knee..';
$originalStrings[] = 'Gryphon. \'Then, you know,\' Alice gently remarked; \'they\'d have been was not much like keeping so close to her, still it was growing, and very soon finished it off. * * * * * * * * * * * * * * * * * * * * * * * * * * * CHAPTER II. The Pool of Tears \'Curiouser and curiouser!\' cried Alice in a ring, and begged the Mouse was speaking, and this was the same thing, you know.\' \'And what are YOUR shoes done with?\' said the King, \'and don\'t look at the other side. The further off from England the nearer is to France-- Then turn not pale, beloved snail, but come and join the dance. Will you, won\'t you, will you, won\'t you, will you join the dance? Will you, won\'t you, will you join the dance. Will you, won\'t you, will you, won\'t you, will you, won\'t you join the dance. Would not, could not, would not, could not, would not, could not, would not give all else for two reasons. First, because I\'m on the same side of WHAT?\' thought Alice; \'I might as well as if she did not like to go down the little door into that lovely garden. First, however, she went round the court with a little now and then, if I might venture to ask any more questions about it, you may stand down,\' continued the Hatter, who turned pale and fidgeted. \'Give your evidence,\' the King put on one knee..';
$originalStrings[] = 'Mock Turtle replied in a moment: she looked up, and there was no \'One, two, three, and away,\' but they were mine before. If I or she should meet the real Mary Ann, and be turned out of the lefthand bit. * * * * * * * * * * * * * * * * * * * * * CHAPTER II. The Pool of Tears \'Curiouser and curiouser!\' cried Alice again, for really I\'m quite tired of sitting by her sister sat still just as she ran..';
$originalStrings[] = 'White Rabbit read:-- \'They told me he was in such a thing before, but she got up, and there they are!\' said the Gryphon. \'I\'ve forgotten the words.\' So they went on just as if he were trying which word sounded best. Some of the lefthand bit. * * * * * * * * * * * * * * * * * \'Come, my head\'s free at last!\' said Alice a good character, But said I could shut up like telescopes: this time she heard was a real nose; also its eyes were getting so far off). \'Oh, my poor little feet, I wonder who will put on one of the soldiers did. After these came the guests, mostly Kings and Queens,.';

foreach($originalStrings as $originalString)
{
    echo "Original: " . $originalString;
    echo "<br>Original length: " . strlen($originalString);
    echo "<br>Truncated: ";
    $truncated = \HtmlTruncator\Truncator::truncate($originalString, 80/*, ['length_in_chars'=>true]*/);
    echo $truncated;
    echo "<br>Truncated length: " . strlen($truncated);
    echo '<hr>';
}

The above code outputs:

Original: * * * Gryphon. 'Then, you know,' Alice gently remarked; 'they'd have been was not much like keeping so close to her, still it was growing, and very soon finished it off. * * * * * * * * * * * * * * * * * * * * * * * * * * * CHAPTER II. The Pool of Tears 'Curiouser and curiouser!' cried Alice in a ring, and begged the Mouse was speaking, and this was the same thing, you know.' 'And what are YOUR shoes done with?' said the King, 'and don't look at the other side. The further off from England the nearer is to France-- Then turn not pale, beloved snail, but come and join the dance. Will you, won't you, will you, won't you, will you join the dance? Will you, won't you, will you join the dance. Will you, won't you, will you, won't you, will you, won't you join the dance. Would not, could not, would not, could not, would not, could not, would not give all else for two reasons. First, because I'm on the same side of WHAT?' thought Alice; 'I might as well as if she did not like to go down the little door into that lovely garden. First, however, she went round the court with a little now and then, if I might venture to ask any more questions about it, you may stand down,' continued the Hatter, who turned pale and fidgeted. 'Give your evidence,' the King put on one knee..
Original length: 1282
Truncated: …
Truncated length: 3
-
Original: Gryphon. 'Then, you know,' Alice gently remarked; 'they'd have been was not much like keeping so close to her, still it was growing, and very soon finished it off. * * * * * * * * * * * * * * * * * * * * * * * * * * * CHAPTER II. The Pool of Tears 'Curiouser and curiouser!' cried Alice in a ring, and begged the Mouse was speaking, and this was the same thing, you know.' 'And what are YOUR shoes done with?' said the King, 'and don't look at the other side. The further off from England the nearer is to France-- Then turn not pale, beloved snail, but come and join the dance. Will you, won't you, will you, won't you, will you join the dance? Will you, won't you, will you join the dance. Will you, won't you, will you, won't you, will you, won't you join the dance. Would not, could not, would not, could not, would not, could not, would not give all else for two reasons. First, because I'm on the same side of WHAT?' thought Alice; 'I might as well as if she did not like to go down the little door into that lovely garden. First, however, she went round the court with a little now and then, if I might venture to ask any more questions about it, you may stand down,' continued the Hatter, who turned pale and fidgeted. 'Give your evidence,' the King put on one knee..
Original length: 1275
Truncated: …
Truncated length: 3
-
Original: Mock Turtle replied in a moment: she looked up, and there was no 'One, two, three, and away,' but they were mine before. If I or she should meet the real Mary Ann, and be turned out of the lefthand bit. * * * * * * * * * * * * * * * * * * * * * CHAPTER II. The Pool of Tears 'Curiouser and curiouser!' cried Alice again, for really I'm quite tired of sitting by her sister sat still just as she ran..
Original length: 400
Truncated: …
Truncated length: 3
-
Original: White Rabbit read:-- 'They told me he was in such a thing before, but she got up, and there they are!' said the Gryphon. 'I've forgotten the words.' So they went on just as if he were trying which word sounded best. Some of the lefthand bit. * * * * * * * * * * * * * * * * * 'Come, my head's free at last!' said Alice a good character, But said I could shut up like telescopes: this time she heard was a real nose; also its eyes were getting so far off). 'Oh, my poor little feet, I wonder who will put on one of the soldiers did. After these came the guests, mostly Kings and Queens,.
Original length: 586
Truncated: …
Truncated length: 3

Changing the $length argument seems to affect it too. Try 10 words or 300 words and it works. But at 80 like above it breaks.

When length_in_chars is enabled...

  • 320 characters $length breaks all four examples
  • 100 characters $length works for all
  • 400 characters $length works with the third example, and breaks the rest.

hi2u avatar Jan 13 '17 19:01 hi2u