Truncate Text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Truncate Text

Post 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?
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

You have to use substr()

Code: Select all

<?php
//cut the string
$substring = substr($thread_title,0,11);
//adding the ...
echo $substring.'...';
?>
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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.
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

It's the best to let mysql do it, especialy if the mysql server is running on a other computer.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

what about this?

Post 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.
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post 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 "..."
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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]
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Doesn't work...

Post 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) ? '...' : '';
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post 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;
}
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: Doesn't work...

Post 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
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Avatar... Thanks..

Post 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;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Last edited by twigletmac on Wed May 14, 2003 3:48 pm, edited 1 time in total.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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,
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Thanks guys...

Post 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;
Post Reply