Ever needed to shorten copy to blurb or (cutline) a header? This code will append ‘…’ to a string if its greater than $Length. Just set that variable and $Str and your set.
[code lang=”php”]
$dataLength = 11; // Set this to the length of the string before clipping
$dataString = ‘The red fox jumped over the lazy dog.’;
if(strlen($dataString) > $dataLength){
preg_match(’/[a-zA-Z0-9]{0,’.$dataLength.’}/’, $dataString, $M);
$Output = $M . ‘…’;
}
[/code]
Now this is a simplified way to count per characters which may be great for a string or single line such as a header but you want to cut a blurb, try this:
[code lang=”php”]
$dataLength = 3; // Set this to the amount of words before clipping
$dataString = ‘The red fox jumped over the lazy dog.’;
$Words = explode(” “, $dataString);
$whileCount = 0;
do {
echo $Words[$whileCount] . ” “;
$i++;
} while ($whileCount < $dataLength );
echo “…”;
[/code]