judev / php-htmltruncator Goto Github PK
View Code? Open in Web Editor NEWWant to truncate an HTML string properly? This is a PHP port of the html_truncator rubygem.
License: MIT License
Want to truncate an HTML string properly? This is a PHP port of the html_truncator rubygem.
License: MIT License
This is just so people can rely on a particular version without worrying about you changing the functionality.
Thanks for a great package.
When I run unicode characters through truncate it doesn't come through correctly
Expected:
This is the greek letter beta: β
Actual:
This is the greek letter beta: β
Hey,
There seems to be a problem with adjacent HTML tags.
For example, try to truncate this to 25 characters:
<p>Lorem Ipsum</p><p>Lorem <div>Lorem Ipsum</div> Ipsum</p>
and check the output. You should notice an issue with the markdown.
If I change the second paragraph to h1, everything seems to be fine.
Usages of HtmlTruncator, which is apparently no longer maintained, result in a fatal PHP error when the deprecated PHP extension iconv is no longer available. This is because the HTML5Lib sub-dependency, which is also not maintained, relies on iconv.
Fatal error: Uncaught Error: Class 'HTML5Lib\Exception' not found in /.../vendor/electrolinux/php-html5lib/src/HTML5Lib/InputStream.php:81
Using version ^1.0 for judev/php-htmltruncator
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- judev/php-htmltruncator 1.0.6 requires electrolinux/php-html5lib dev-master -> no matching package found.
- judev/php-htmltruncator 1.0.5 requires electrolinux/php-html5lib dev-master -> no matching package found.
- judev/php-htmltruncator 1.0.4 requires electrolinux/php-html5lib dev-master -> no matching package found.
- judev/php-htmltruncator 1.0.3 requires electrolinux/php-html5lib dev-master -> no matching package found.
- judev/php-htmltruncator 1.0.2 requires electrolinux/php-html5lib dev-master -> no matching package found.
- judev/php-htmltruncator 1.0.1 requires electrolinux/php-html5lib dev-master -> no matching package found.
- judev/php-htmltruncator 1.0.0 requires electrolinux/php-html5lib dev-master -> no matching package found.
- Installation request for judev/php-htmltruncator ^1.0 -> satisfiable by judev/php-htmltruncator[1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.0.6].
The library removes non-self-closing tags without text, such as an iframe.
For example:
* string: <div class="media-pull-center media-width-full"><div class="embed-responsive embed-responsive-16by9"><iframe src="https://link.brightcove.com/services/player/" frameborder="0" allowfullscreen></iframe></div><div class="content-item__credit pull-right">Credit</div><div class="content-item__caption clearfix">Caption</div></div>
* length: 500
* result: <div class="media-pull-center media-width-full"><div class="content-item__credit pull-right">Credit</div><div class="content-item__caption clearfix">Caption</div></div>
* desired result: <div class="media-pull-center media-width-full"><div class="embed-responsive embed-responsive-16by9"><iframe src="https://link.brightcove.com/services/player/" frameborder="0" allowfullscreen></iframe></div><div class="content-item__credit pull-right">Credit</div><div class="content-item__caption clearfix">Caption</div></div>
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.
$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>';
}
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...
Hello,
Any plans to publish this on PHP Package repo (https://packagist.org/)?
Thanks!
Hi, Firstly congratulations on the project. Its simply awesome!
However, I'm getting a strange a really strange issue and I ran out of options.
Default charset in php.ini is set to UTF-8, same symfony setting.
When I get the data from db all is fine, but after running this trough htmltruncator the encoding is wrong and I'm getting weird characters.
any idea what may cause this ? Please help
Composer error:
Your requirements could not be resolved to an installable set of packages. Problem 1 - The requested package judev/php-htmltruncator 1.2.* exists as judev/php-htmltruncator[1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.0.6, 1.1.0, dev-master] but these are rejected by your constraint.
The Packagist website still shows 1.1.0 as well.
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.