cpugeek Posted August 7, 2008 Share Posted August 7, 2008 I'm working on a website that hosts images in a gallery.but sometimes the names get too long and ruin the output of the page by pushing things over or folding the name over a couple of lines.what i was wondering is how would i shorten the names when displayed? like when the name were to reach a certain length the rest of the name would be replaced by 3 dots.here's an example of what i'm talking about. Link to comment Share on other sites More sharing options...
Ingolme Posted August 7, 2008 Share Posted August 7, 2008 You would have to use PHP, you'd do something like this: // $str is the variable with the string in itif(strlen($str) > 25) { $str = substr($str,0 25) . "...";} Link to comment Share on other sites More sharing options...
justsomeguy Posted August 7, 2008 Share Posted August 7, 2008 function wrap_str($str, $len){ if (strlen($str) <= $len) return $str; return substr(0, $len - 3) . '...';} You can add additional code if you only want it to break on a word boundary. Link to comment Share on other sites More sharing options...
jlhaslip Posted August 11, 2008 Share Posted August 11, 2008 if ( strlen($name) >= 27) { $name = substr($name, 7, 22); // strip off the http:// if ( strlen($name) >= 27) { // grab the next 15 characters $front = substr($name, 0, 15); } $back = substr($name, strlen($string)-5, 5); // grab the last 5 characters $mid = "..."; // stuff the middle with an ellipse character $name = $front . $mid . $back; // glue the parts back } Here is a portion of a function I wrote for a script. Checks the length, grabs the first 15 characters, the last 5, and strings them together will an ellipse to show some parts are missing.(only if the length is over 20 characters) Adjust the numbers to suit your needs. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.