Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Would like to shorten the length of string returned in recordset

Status
Not open for further replies.

dreamaz

Technical User
Dec 18, 2002
184
CA
Hi Everyone,

I have a field in a record set that has 200 characters in it. I woul dlike to shorten it to 100 and add ... so I can click on it for more detaiils. I already have a master/detail page setup and everything is done.

Currently i have:

<?php echo $row_GetKnownIssue['AlertDescription']; ?>

which displays the field and already is a link to a detailed page.

Thanks

 
Code:
if (strlen($row_GetKnownIssue['AlertDescription'] > 100)
{
   $AlertDesc = substr($row_GetKnownIssue['AlertDescription'], 0, 100) . "...";
}
This will cut strings that are longer than 100 characters at 100th character and display three dots at the end.
 
Thanks for the quick reply.

I tried that and got: Parse error: parse error, unexpected '{' in /srv/ on line 31

when i removed the { i got unexpected T_VARIABLE

thanks
 
Many times when PHP indicates an error on a particular line, the real error is in a line before the one pointed to. In this case the line above is missing a closing parenthesis:
Code:
if (strlen($row_GetKnownIssue['AlertDescription'][red][b])[/b][/red] > 100)
{
   $AlertDesc = substr($row_GetKnownIssue['AlertDescription'], 0, 100) . "...";
}

Please remember that code posted in these answers should be checked for correctness. It is often entered without checking.

Ken
 
By the way, if you are doing it with that, the string might just look funny.
You can also do by phrases. Let's say you want to get only 30 phrases, you can do the following:

$all_phrases = explode (" ",$row_GetKnownIssue['AlertDescription']);
$newstring = "";
if (count($all_phrases) > 30)
{
for ($i=0; $i<30; $i++)
$newstring .= $all_phrases[$i];
$newstring .= "...";
$AlertDesc = $newstring;
} else
{
$AlertDesc = $row_GetKnownIssue['AlertDescription'];
}
 
Code:
 $newstring .= $all_phrases[$i];
Should read:
Code:
 $newstring .= " $all_phrases[$i]";

Otherwise the words will be all pushed together.

How would you get it to count upto say 30 phrases and then stop at a full stop "." or exclamation mark "!" so it is nice and tidy and doesn't stop mid sentence?

Reality is built on a foundation of dreams.
 
I think I figured it out!!!
Code:
function summarize($art, $limit){
  								$tok = strtok($art, " ");
  								while($tok){
   									$text .= " $tok";
   									$words++;
   									if(($words >= $limit) && ((substr($tok, -1) == "!")||(substr($tok, -1) == ".")))
     								break;
   									$tok = strtok(" ");
  									}
									return ltrim($text);
								}
								
							//MySQL query to extract articles from the database 
$sql = mysql_query("SELECT * FROM articles"); 

while ($row = mysql_fetch_array($sql)) {
								// give a name to the fields
$art = $row['art_main'];
									//limit count of spacing phrases				
$limit = 40;

//print out the row with the condensed string
print("<tr><td>".summarize($art, $limit)."</td></tr>");
}
Hope this helps


Reality is built on a foundation of dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top