note 110370 deleted from function.substr by googleguy
| From: | googleguy@php.net | Date: | Tue, 16 Oct 2012 11:54:37 +0000 |
| Subject: | note 110370 deleted from function.substr by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191323@lists.php.net to get a copy of this message | ||
Note Submitter: Nico -at- www.nokes.de
----
Easy way to short a string
<?php
function short_me($string, $long){
$short_string = (strlen($string) > $long ? substr($string, 0, $long) . "…"
: $string);
return $short_string;
}
$text = "Hello, this is an example."; // Lenght: 20
// Output
echo $text; // -> Hello, this is an example.
echo short_me($text, 8); // -> Hello, tâ¦
echo short_me($text, 5); // -> Helloâ¦
echo short_me($text, -5); // -> Hello, this is an exaâ¦
?>
Thanks to firepanther (www.coderz.pro)