note 54441 modified in function.substr by danbrown

From: Date: Fri, 28 Nov 2008 20:22:46 +0000
Subject: note 54441 modified in function.substr by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147205@lists.php.net to get a copy of this message
This function shortens the string down to maximum lengt defined in $max_lengt. If the string is longer the function finds the last occurance of a space and adds three dots at the end to illustrate that it is more text. If the string is without spaces it stops at exacly max lengt, also adding three dots. If the string is shorter than max lengt it returns the string as it is. This is useful for previewing long strings. <?php function str_stop($string, $max_length){ if (strlen($string) > $max_length){ $string = substr($string, 0, $max_length); $pos = strrpos($string, " "); if($pos === false) { return substr($string, 0, $max_length)."..."; } return substr($string, 0, $pos)."..."; }else{ return $string; } } ?> --was-- This function shortens the string down to maximum lengt defined in $max_lengt. If the string is longer the function finds the last occurance of a space and adds three dots at the end to illustrate that it is more text. If the string is without spaces it stops at exacly max lengt, also adding three dots. If the string is shorter than max lengt it returns the string as it is. This is useful for previewing long strings. function str_stop($string, $max_length){ if (strlen($string) > $max_length){ $string = substr($string, 0, $max_length); $pos = strrpos($string, " "); if($pos === false) { return substr($string, 0, $max_length)."..."; } return substr($string, 0, $pos)."..."; }else{ return $string; } } http://php.net/manual/en/function.substr.php

« previous php.notes (#147205) next »