chrisstump
2/9/18 at 10:33 am
PHP
PHP / CSS: Truncate String and Add Ellipsis
This is a helpful utility for truncating a given string to a specific length and adding an ellipsis...
// if the input string is longer than 50 characters, truncate the string and add ... otherwise, return the original string $output = strlen($input) > 50 ? substr($input,0,50)."..." : $input; // you can also achieve a similar effect using pure CSS with the following class: .ellipsis { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } // note the CSS solution above requires a fixed width element // more info available here: // https://stackoverflow.com/questions/11434091/add-if-string-is-too-long-php
Copy Snippet to Clipboard
Don't Have an Account?
Stop digging through old code examples. Get organized. Get Code Snipper.
Learn More
Start Free 14 Day Trial