Copy link to clipboard
Copied
language: php
Question: In regards to the master page, does anyone know how to limit the character count to only display 250 in Dreamweaver CS4?
That's easily fixed. Use this custom function instead:
function extractChars($text, $chars = 250) {
if (strlen($text) < $chars) {
return $text;
} else {
$pos = strpos($text, ' ', $chars);
return substr($text, 0, $pos) . '...';
}
}
Put that function definition in your page, and use it like this:
echo extractChars($row_recordsetName['fieldName']);
If the text is less than 250 characters, you'll just get the full text. If it's more than 250, you get the text shorte
...Copy link to clipboard
Copied
What do you mean by "master page"? Are you trying to limit the number of characters that can be inserted into a text area? Or do you want to limit the number of characters displayed when retrieving a record from a database?
Copy link to clipboard
Copied
I want to limit the number of characters displayed when retrieving a record from a database.
jeff
Copy link to clipboard
Copied
Depends on what server language you are using. In CF it would be something like this...
<cfoutput>#Left(Recordset.FieldName, 250)#</cfoutput>
Lawrence *Adobe Community Expert*
www.Cartweaver.com
Complete Shopping Cart Application for
Dreamweaver, available in ASP, PHP and CF
Copy link to clipboard
Copied
I"m using php for my language.
Copy link to clipboard
Copied
Hi Flyboy,
Here is a link to look over the function: http://us3.php.net/manual/en/function.substr.php
$string = $row_Recordset['dbfield'];
$output = substr($string, 0, 255);
echo $output;
Copy link to clipboard
Copied
Using substr() will definitely do it, but it will cut off the string at exactly the number of characters entered as the third argument. A more elegant way of doing it is to find the first space after 250 characters, and then add an elipsis like this:
$string = $row_recordsetName['fieldName'];
// get the first space after 250 characters
$space = strpos($string, ' ', 250);
// cut off the string at the space and add an ellipsis
echo substr($string, 0, $space) . '...';
Copy link to clipboard
Copied
hmm, let me try. I will keep you posted.
Copy link to clipboard
Copied
Ahh, it worked. However, i did notice that if there are less than 250 characters in that database table an error message will appear on that line (see below).
Copy link to clipboard
Copied
That's easily fixed. Use this custom function instead:
function extractChars($text, $chars = 250) {
if (strlen($text) < $chars) {
return $text;
} else {
$pos = strpos($text, ' ', $chars);
return substr($text, 0, $pos) . '...';
}
}
Put that function definition in your page, and use it like this:
echo extractChars($row_recordsetName['fieldName']);
If the text is less than 250 characters, you'll just get the full text. If it's more than 250, you get the text shortened.
The function has an optional second argument, which determines the number of characters that you want to use as the limit. So, if you want only the first hundred, do this:
echo extractChars($row_recordsetName['fieldName'], 100);
Copy link to clipboard
Copied
Sweet!
Thanks..