I have a news feature on a website and have a page that shows all of the news and a link to the actual page of each individual news. On first page that lists all the news i would like to limit the amount of Characters that is echoed from the database field. So from the code below where it states

echo $row;

that is where i need to limit the amount of characters

if you could help that would be great.

<?php
mysql_connect("","","");
mysql_select_db("");
$result = mysql_query("SELECT * FROM news ORDER BY id desc") 
or die(mysql_error());  

echo "<table class = feature >";
echo "<tr> <th></th> <th></th> <th></th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	// Print out the contents of each row into a table
	echo "<tr><td>";
	echo $row['Date'];
	echo "</td><td>";
	echo "<a href='".$row['Link']."'>".$row['Title']."</a>";
	echo "</td><td>";	 
    echo $row['News'];
	echo "</td></tr>";
} 
echo "</table>";
?>

Recommended Answers

All 3 Replies

Do you want to hard limit it to x charachters, or do you want to split it up by the nearest word? Also, does the string contain any kind of formatting/html?

Firstly thanks for your reply.

I would prefer say 20 words and the formatting is all in paragraphs eg html <p> </p>

Member Avatar for P0lT10n

I know there is a code for input type text maxleng, but for p you ha ve to

$string = split("your text", 20);
echo $string;

I dont remember well how the command must be write...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.