Page 1 of 2

Truncate Text

Posted: Mon May 12, 2003 2:24 pm
by AliasBDI
This is the code I have so far:

Code: Select all

<?php
$query = mysql_query("select * from articles where id = 4");
while ($out  =  mysql_fetch_array($query)) &#123;
$thread_title=$out&#1111;"title"];
&#125;
echo "&#123;$thread_title&#125;";
?>
I need the echo statement to truncate the result. In other words, it now returns "BIG LONG TITLE GOES HERE". I need it to shorten the result to "BIG LONG TI...". Truncate it to any number. Then followed by '...'

Any ideas?

Posted: Mon May 12, 2003 2:35 pm
by AVATAr
You have to use substr()

Code: Select all

<?php
//cut the string
$substring = substr($thread_title,0,11);
//adding the ...
echo $substring.'...';
?>

Posted: Mon May 12, 2003 3:42 pm
by pootergeist
or mysqls string functions

http://www.mysql.com/doc/en/String_functions.html

SELECT field1, field2, SUBSTRING_INDEX(title, ' ', 4) AS trun_title FROM .....

that would select the first three (or 4) words from title and return it as trun_title

or

SUBSTRING(title,0,35) AS trun_title

would be the first 35 characters.

Posted: Tue May 13, 2003 1:49 am
by []InTeR[]
It's the best to let mysql do it, especialy if the mysql server is running on a other computer.

what about this?

Posted: Wed May 14, 2003 2:22 pm
by AliasBDI
Here is my statement:

$thread_title = substr("$thread_title", 0, 20) . "...";

This works, but it displays the '...' after the 20 characters no matter how many characters there are.

So, if the text is: Joe Bob -> it shows: Joe Bob...
If the text is: I Am My Brother's Keeper -> it shows: I Am My Brother's Ke...

I want it to only show the '...' IF it is truncated.

Posted: Wed May 14, 2003 2:30 pm
by AVATAr
use strlen() to find out the length of the string, then if this length is bigger than the length you want, truncate it and add the "..."

Posted: Wed May 14, 2003 2:35 pm
by twigletmac
Why not truncate the string when getting it from the database? Making MySQL do the work is a good thing rather than returning information you don't need...

However, to answer your question, you need to look at the str_length before you add the '...':

Code: Select all

$truncated_text .= (strlen($truncated_text) == 20) ? '...' : '';
Mac

[edit: urgh, too slow but I would like to make the point about using MySQL for the truncation]

Doesn't work...

Posted: Wed May 14, 2003 3:02 pm
by AliasBDI

Code: Select all

$truncated_text .= (strlen($truncated_text) == 20) ? '...' : '';
Doesn't work. It will not even display the text at all. I also tried this w/out the period in it:

Code: Select all

$truncated_text = (strlen($truncated_text) == 20) ? '...' : '';

Posted: Wed May 14, 2003 3:06 pm
by AVATAr
something like this

Code: Select all

<?php

$maxlength = 20;

if (strlen($thread_title) > $maxlength){
    $truncatedtext = substr($thread_title, 0, $maxlength) . "..."; 
} else {
     $truncatedtext = $thread_title;
}
?>

Re: Doesn't work...

Posted: Wed May 14, 2003 3:28 pm
by twigletmac
AliasBDI wrote:

Code: Select all

$truncated_text .= (strlen($truncated_text) == 20) ? '...' : '';
Doesn't work. It will not even display the text at all. I also tried this w/out the period in it:

Code: Select all

$truncated_text = (strlen($truncated_text) == 20) ? '...' : '';
It was an example which assumed that the truncated text had been set to a variable called $truncated_text - you already had the code for doing that so I didn't repeat it. In your code the truncated text was set to $thread_title.

The period before the equal sign is used so that the following string will be added to the original.

Mac

Avatar... Thanks..

Posted: Wed May 14, 2003 3:28 pm
by AliasBDI
Thanks for your help. I am a newbie and I cannot seem to make this work is the the code in its entirety. How would I implement your code?

Code: Select all

function do_lastposts() &#123;
global $DB, $ibforums, $root_path, $templates_dir, $std, $INFO;


$admin_forums = '(0)'; // IDs of the Forums you want to hide;
    $limit        = "5"; // Number of posts to show
$prefix    =  $INFO&#1111;'sql_tbl_prefix'];


$template = load_template("lastposts.html");

    $to_echo = "";

$DB->query( "SELECT p.pid, p.author_name, p.post_date, p.forum_id, p.topic_id, p.author_id, t.title ,f.name FROM ".$prefix."posts p ,   ".$prefix."topics t, ".$prefix."forums f WHERE t.tid=p.topic_id AND t.forum_id=f.id AND p.forum_id NOT IN ".$admin_forums."  ORDER BY pid DESC LIMIT 0,".$limit." " );
if ( ! $DB->get_num_rows() )
&#123;
 fatal_error("Could not get the information from the database");
&#125;

while( $out = $DB->fetch_row($query) ) &#123;  
 $thread_title = $out&#1111;'title'];
 $forum_name =$out&#1111;'name'];
 $author             = $out&#1111;'author_name'];   
 $out&#1111;'post_date'] = $std->get_date( $out&#1111;'post_date'], 'LONG' );
 $date               = $out&#1111;'post_date'];
 $to_echo  .= parse_template( $template,array (
    
 'thread_url' => $ibforums->base_url."?act=ST&f=".$out&#1111;'forum_id']."&t=".$out&#1111;'topic_id']."&hl=&#entry".$out&#1111;'pid'],
 'thread_title' => $thread_title,
 'forum_url' => $ibforums->base_url."?act=SF&f=".$out&#1111;'forum_id'],
 'forum_name' => $forum_name,
 'date'       => $date,
 'author'     => $author,
 'profile_link'   => $ibforums->base_url."?act=Profile&CODE=03&MID=".$out&#1111;'author_id'], ) );
&#125;

Posted: Wed May 14, 2003 3:41 pm
by twigletmac
Within the SQL statement you could change:

Code: Select all

t.title
to

Code: Select all

SUBSTRING(t.title, 0, 20) AS title
and then instead of

Code: Select all

$thread_title = $out['title'];
you could do

Code: Select all

$thread_title = $out['title'];
if (strlen($thread_title) == 20) {
     $thread_title .=  '...';
}
Mac

Posted: Wed May 14, 2003 3:47 pm
by pootergeist
betcha posted the substring() mysql code after reading my post huh? ;)

that should be

SUBSTRING(t.title,0,20) AS title,

note the 0,

Posted: Wed May 14, 2003 3:50 pm
by twigletmac
pootergeist wrote:betcha posted the substring() mysql code after reading my post huh? ;)

that should be

SUBSTRING(t.title,0,20) AS title,

note the 0,
Yup, copied it out of your code because I always forget the order of stuff for the substring() functions and figured you'd probably looked it up when you posted... I've corrected the code above.

Mac

Thanks guys...

Posted: Wed May 14, 2003 4:04 pm
by AliasBDI
The text still will not show... here is my code now...sorry about this head ache:

Code: Select all

function do_lastposts() &#123;
	global $DB, $ibforums, $root_path, $templates_dir, $std, $INFO;
	

	$admin_forums = '(0)'; // IDs of the Forums you want to hide;
    	$limit        = "5"; // Number of posts to show
	$prefix 	  =  $INFO&#1111;'sql_tbl_prefix'];

	
	$template = load_template("lastposts.html");

    	$to_echo = "";

	$DB->query( "SELECT p.pid, p.author_name, p.post_date, p.forum_id, p.topic_id, p.author_id, SUBSTRING(t.title,0,20) AS title, f.name FROM ".$prefix."posts p ,   ".$prefix."topics t, ".$prefix."forums f WHERE t.tid=p.topic_id AND t.forum_id=f.id AND p.forum_id NOT IN ".$admin_forums."  ORDER BY pid DESC LIMIT 0,".$limit." " );
	if ( ! $DB->get_num_rows() )
	&#123;
		fatal_error("Could not get the information from the database");
	&#125;

	while( $out = $DB->fetch_row($query) ) &#123;
		$thread_title = $out&#1111;'title']; 
		if (strlen($thread_title) == 20) &#123; 
		$thread_title .=  '...'; 
		&#125;

		$maxlength = 20; 
		if (strlen($thread_title) > $maxlength)&#123; 
		$truncatedtext = substr($thread_title, 0, $maxlength) . "..."; 
		&#125; else &#123; 
		$truncatedtext = $thread_title; 
		&#125;
 		
		//$thread_title = (strlen($thread_title) == 20) ? '...' : '';
		$forum_name       = $out&#1111;'name'];
		$author           = $out&#1111;'author_name'];
            $author           = substr("$author", 0, 10);			
		$out&#1111;'post_date']	= $std->get_date( $out&#1111;'post_date'], '2DIGITS' );
		$date             = $out&#1111;'post_date'];
		$to_echo         .= parse_template( $template,array (
				 
		'thread_url' => $ibforums->base_url."?act=ST&f=".$out&#1111;'forum_id']."&t=".$out&#1111;'topic_id']."&hl=&#entry".$out&#1111;'pid'],
		'thread_title' => $thread_title,
		'forum_url' => $ibforums->base_url."?act=SF&f=".$out&#1111;'forum_id'],
		'forum_name' => $forum_name,
		'date'       => $date,
		'author'     => $author,
		'profile_link'   => $ibforums->base_url."?act=Profile&CODE=03&MID=".$out&#1111;'author_id'],	) );
	&#125;