Jump to content

Cutting off a sentence


XionX

Recommended Posts

alright i have this code:

 

<?php 
	   ConnectToforum();
	  $sql = "select * from `thread` where `forumid` = '48' order by `threadid` desc limit 0,30";
	  $query = mysql_query($sql);
	  while ($row = mysql_fetch_array($query)) {
	    echo "[<a href=\"forum/showthread.php?t=".$row['threadid']."\">View</a>] ".$row['title']." <br />";

		"<p />";
	  }
	  mysql_close();
	  ?>

 

i want to know how to make a sentence it outputs to cut off and replace it with "..." after it extends the width of a table or after it exceeds a certain amount of characters.

 

like if the sentence were this:

"How now brown cow?"

if it only allowed 10 characters to be shown it would output this:

"How now..."

so it would actually only show the first 7 characters of the sentence and then the last 3 would be "..."

 

i'm fine with this being done by either a certain amount of characters or the width of the table, any help would be greatly appreciated, thanks

Link to comment
https://forums.phpfreaks.com/topic/58356-cutting-off-a-sentence/
Share on other sites

<?php
echo substr('abcdef', 1);     // bcdef
echo substr('abcdef', 1, 3);  // bcd
echo substr('abcdef', 0, 4);  // abcd
echo substr('abcdef', 0, ;  // abcdef
echo substr('abcdef', -1, 1); // f

// Accessing single characters in a string
// can also be achived using "curly braces"
$string = 'abcdef';
echo $string{0};                 // a
echo $string{3};                 // d
echo $string{strlen($string)-1}; // f

?> 

http://us2.php.net/substr

Im not quite sure what your looking for, but f.ex in my news script I use the MYSQL LEFT function. With this function you can predefine in the mysql query how many characters you want to extract from the row in MySQL. This works great when you want a "read more" function on f.ex your news.

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.