Giter Site home page Giter Site logo

mrcgrtz / php-shorten Goto Github PK

View Code? Open in Web Editor NEW
11.0 1.0 2.0 380 KB

💬 Provides additional truncation functions in PHP.

Home Page: https://packagist.org/packages/marcgoertz/shorten

License: MIT License

PHP 100.00%
php wordsafe-truncation html markup shorten truncation

php-shorten's Introduction

Shorten

Provides additional truncation functions in PHP.

Test Coverage Status Packagist PHP Version Support Packagist Downloads Packagist Stars MIT License

Installation

I recommend using Composer for installing and using Shorten:

composer require marcgoertz/shorten

Of course you can also just require it in your scripts directly.

Usage

<?php

use Marcgoertz\Shorten\Shorten;

$shorten = new Shorten();
print $shorten->truncateMarkup('<a href="https://example.com/">Go to example site</a>', 10);
?>

Output:

<a href="https://example.com/">Go to exam</a>

Functions

truncateMarkup(
    string $markup,
    int $length = 400,
    string $appendix = '',
    bool $appendixInside = false,
    bool $wordsafe = false
): string
  • String $markup: Text containing markup
  • Integer $length: Maximum length of truncated text (default: 400)
  • String $appendix: Text added after truncated text (default: '…')
  • Boolean $appendixInside: Add appendix to last content in tags, increases $length by 1 (default: false)
  • Boolean $wordsafe: Wordsafe truncation (default: false)
  • String $delimiter: Delimiter for wordsafe truncation (default: ' ')

License

MIT © Marc Görtz

php-shorten's People

Contributors

dependabot[bot] avatar lostfocus avatar mrcgrtz avatar tobyzerner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

php-shorten's Issues

Handle non-XML styled self-closing tags

Shorten->truncateMarkup('<a href="https://example.com/">Go to<br />example site</a>', 10);
// ✅ '<a href="https://example.com/">Go to<br />examp</a>…'

Shorten->truncateMarkup('<a href="https://example.com/">Go to<br />example site</a>', 10);
// 🛑 '<a href="https://example.com/">Go to<br>examp</br></a>…'

Self-closing tags like <br>, <img> et al. should be handled properly.

Option to prevent html entities in result

When displaying a truncated text from this library with Twig, it displaying html entities because Twig safely encodes &.

Actually I expected UTF-8 characters are returned as is:

truncateMarkup('<p>PHP éléphant</p>', 7)

Expected:

PHP élé...

Actually:

PHP &eacute;l&eacute;

I understand this makes sense, but there should be a way to disable html entities, i.e by calling html_entity_decode on truncated text before returning it (which is what we are currently doing on our side).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.