View Full Version : Adding periods to the end

01-20-2004, 03:27 AM

What I am wanting to do on the side where the news is, I am wanting to make my script so that it will add before the end three periods like 'Spirit 'ready to rove' acro...' so the news item will only take up one line. I think it is 33 Characters across, including spaces.

All help is welcome.


01-20-2004, 04:28 AM
$title = "Spirit ready to rove across the world";

$title = substr($title,0,30);

$title = $title."...";

01-20-2004, 05:38 PM
Thank You.

01-20-2004, 05:49 PM
To make that more versatile in case you have variable length strings it would be

$title = "Spirit ready to rove across the world";

$title = substr($title,0,strlen($title));

$title .= "...";

02-29-2004, 09:13 PM
Sorry to dig this up, but I need it again. Sorry :(

Anyways, here is my problem. A line can hold 16 + or - characters. I want everything on that line that is over 16 characters to make as if it were 13 and add ... to the end. Spookster snippet is confusing to me.

The code I am wanting to use this one is this.
function topics()
$result = "SELECT id,author,topic,email,mood,date,entry FROM blog ORDER BY id DESC LIMIT 10";
$sql = mysql_query($result) or die(mysql_error());
if(mysql_num_rows($sql) == "0"){
echo "No information in the database!";
while($row = mysql_fetch_assoc($sql)){
$this->topic = $row['topic'];
$this->id = $row['id'];
echo "<div><a href='des.php?mode=topic&id=$this->id'>$this->topic</a></div>\n";


I want to make $this->topic the one with the ... on the end.

Can anyone help?

Once again, sorry to dig this up. Thanks

03-01-2004, 12:24 AM
What's confusing about it? All I did was call the substring function which takes 3 parameters.

substr([string to be processed],[index number of character to start at],[index number of character to stop at])

so in mine I said


Take the substring of the string contained in the variable $title from index 0 to the index located at the end of the string which is retrieved by grabbing the length of the string.

03-01-2004, 12:47 AM
Spookster, your snippet is confusing to me as well. If I understood Stevie correctly, he wants to truncate a string at a determined index, if it is longer than 16 characters. What your snippet does is to retrieve a substring from start to end of the string - essentially, it just makes a copy and appends the "...".

Here's my take at it:

if (strlen($row['topic']) > 16) {
$this->topic = substr($row['topic'], 0, 13) . '...';
} else {
$this->topic = $row['topic'];

EDIT: Added "else" branch.

03-01-2004, 04:16 AM
Works great, mordred. Thanks

03-01-2004, 04:25 AM
don't forget that some letters are fatter than others with some fonts. it is hard to accomadate all combinations, but using a test letter like "D" or whatever letter looks fattest in the font your site prefers. then do the column text width based on that.