Jump to content

[SOLVED] trim a string from db


wrathican

Recommended Posts

<?php
$string = "This is a test of the truncate function";

function truncate($string, $max_length)
{
if(strlen($string) > $max_length-3)
{
	$string = substr($string, 0, $max_length-3).'...';
}

return $string;
}

echo truncate($string, 15); // This is a te...

function truncate2($string, $max_words)
{
$str = null;
if(str_word_count($string) > $max_words)
{
	preg_match('/^\s*(?:\S+\s*){1,'. (int) $max_words .'}/', $string, $matches);
	$str = trim($matches[0]).'...';
}
else {
	$str = $string;
}

return $str;
}

echo truncate2($string, 4); // This is a test...
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.