Limit number of characters

Hi all,

I’m outputting a description to a table, and only want a few characters showing and then an elipsis on the end (…).

Can anybody help me with this.

The bit that needs to be reduced is > ".$row[“category_Description”].


<?php
$q=mysql_query("select * from category order by category_Name ASC") or die (mysql_error());

while($row=mysql_fetch_assoc($q))

{
echo "<tr>";
echo "<td bgcolor='#888888' width='50%' valign='top'>";
echo "<p style='position:relative; left:9px;'><font size='2' color='#ffffff' face='Verdana,Arial,Helvetica,sans-serif'><a href='admin_categories.php?cat_Id=".$row["cat_Id"]."&index=".$index."' style='color:FFFFFF; font-weight:normal;'>".$row["category_Name"]."</a></font></p>";
echo "</td>";
echo "<td bgcolor='#888888' width='500' valign='top'><div style='position:relative; float:left; width:350px;'><font size='2' color='#ffffff' face='Verdana,Arial,Helvetica,sans-serif' style='position:relative; left:9px;'>".$row["category_Description"]."</font></div><div style='position:relative; float:right; width:150px; right:-45px; border-left:#333333 dotted 1px; height:20px;'><font size='1' color='#ffffff' face='Verdana,Arial,Helvetica,sans-serif' style='position:relative; margin-left:10px;height:20px;'>".$row["cat_DateTime"]."</font></div></td>";
echo "</tr>";
}				
?>

The substr()[/FONT] function is what you would need to use, if you really want to get specific you can use the [FONT=courier new][URL=“http://www.php.net/manual/en/function.substr-replace.php”]substr_replace() function but substr() should do just fine.

OK thank you.

This didnt work, as it brought up an error. but am I on the sort of right path here.


echo "&lt;td bgcolor='#888888' width='500' valign='top'&gt;&lt;div style='position:relative; float:left; width:350px;'&gt;&lt;font size='2' color='#ffffff' face='Verdana,Arial,Helvetica,sans-serif' style='position:relative; left:9px;'&gt;".$row substr("category_Description, -1")."&lt;/font&gt;&lt;/div&gt;
/PHP]

".$row substr("category_Description, -1")." - being the change

yeah… thats not constructed correctly. substr($row[‘category_Description’],0,100)

It would actually be better to shorten this description at the query level, rather than at the PHP level.

$q=mysql_query(“select cat_Id,category_Name,cat_dateTime,SUBSTRING(‘category_Description’,0,100) from category order by category_Name ASC”) or die (mysql_error());

That great thank you for that, it worked a treat and just what I need. Cheers

Just a tip, STOP using multiple echo’s to do one echo’s job, STOP using double quotes and string additions for multi-field output, stop using outdated deprecated elements like FONT and attributes like bgColor which have no business even being in HTML after 1998, etc, etc…

… and good lord what are you doing with all that positioning apart from trying to make a layout that’s broken everywhere?!?


<?php
$q=mysql_query("select * from category order by category_Name ASC") or die (mysql_error());

while($row=mysql_fetch_assoc($q)) {
	echo '
		<tr>
			<th scope="row">
				<a href="admin_categories.php?cat_Id=',$row["cat_Id"],'&index=',$index,'">
					',$row["category_Name"],'
				</a>
			</th>
			<td class="description">
				',substr($row['category_Description'],0,100),'
			</td>
			<td class="date">
				',$row["cat_DateTime"],'
			</td>
		</tr>';
}
?>

EVERYTHING else there belonging in an external stylesheet. Sorry to say it, but you have a poster child for why I say the style attribute should be deprecated… and why double quotes for echo are rubbish… inlining the CSS like that may as well be presentational markup since you’re not leveraging caching models and missing the entire POINT of using CSS in the first place.

Hi,
If the text contains html tags, when you get the substring its posible to have opened tags, which will aaffect the page format, in this case apply strip_tags() to the substring.

Very good point too – definitely seconded. +1 even…

@multichild

To deathshadow60’s point you can echo something that is mixed with single and double quotes using heredoc syntax so something like this would work:


$html =  &lt;&lt;&lt;MCD
&lt;a href='http://something.com'&gt;something&lt;/a&gt;
&lt;p&gt;$_POST["apple"]&lt;/p&gt;
&lt;p&gt;And we took the "fruit and nuts" to the `picnic!`&lt;/p&gt;
MCD;
echo $html;