urodoz / truncate-html
Handle truncate action on HTML strings
Installs: 257 365
Dependents: 9
Suggesters: 0
Security: 0
Stars: 21
Watchers: 5
Forks: 8
Open Issues: 7
Requires
- php: >=5.3.3
Requires (Dev)
- phpunit/phpunit: ~4
- symfony/dependency-injection: ~2.4
- symfony/http-kernel: ~2.4
- twig/twig: ~1
This package is not auto-updated.
Last update: 2024-12-21 16:08:12 UTC
README
PHP library to handle truncate action on HTML strings
Features
- Truncates an HTML string keeping consistency on open/close tags
- No external dependencies.
- PSR-4 compatible.
- Compatible with PHP >= 5.3.3 and
- Integrations for Symfony2 and Twig.
Installation
You can install urodoz/truncateHTML through Composer:
$ composer require urodoz/truncate-html:@stable
Usage
Truncate an HTML string:
use Urodoz\Truncate\TruncateService; $truncateService = new TruncateService(); echo $truncateService->truncate($htmlString, 100); //Truncating to 100 characters
Integrations
Symfony2
TruncateHTML contains a Symfony2 bundle and service definition that allow you to use it as a service in your Symfony2 application.
The code resides in the Urodoz\Truncate\Bridge\Symfony
namespace and you only need to add the bundle class to your AppKernel.php
:
# app/AppKernel.php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Urodoz\Truncate\Bridge\Symfony\UrodozTruncateBundle(), ); // ... } // ... }
You can now use the urodoz_truncate
service everywhere in your application, for example, in your controller:
$truncatedString = $this->get('urodoz_truncate')->truncate($htmlString, 100);
Twig
If you use the Symfony2 framework with Twig you can use the Twig filter truncateHTML
in your templates after you have setup Symfony2 integrations (see above).
{{ truncateHTML(content, 250) }}
If you use Twig outside of the Symfony2 framework you first need to add the extension to your environment:
use Urodoz\Truncate\Bridge\Twig\TruncateExtension; use Urodoz\Truncate\TruncateService; $twig = new Twig_Environment($loader); $twig->addFunction(new TruncateExtension(TruncateService::create()));
You can find more information about registering extensions in the Twig documentation.
Author
License
The MIT License (MIT)
Copyright (c) 2014 Albert Lacarta
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.