Need a hand with shortening a text string

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

Post Reply
mhw
Forum Newbie
Posts: 5
Joined: Thu Jan 01, 2009 2:37 pm

Need a hand with shortening a text string

Post by mhw »

Hello everyone, the name's Christian..i'm 27 years old and live in the UK.

I am creating a 'portal' for one of my Wordpress powered blogs. So far i've managed to make the portal display the number of articles and the number of comments on the blog. I've also managed to code it up so I can display the title of the most recent blog.

However, my problem is that the space the recent post title has to fit in isn't very large...about 50 characters i'd say. I've looked into shortening text strings but haven't had an awful lot of luck.

Here's the code i'm using to call the recent post title:

Code: Select all

<?php
// Call the database connection settings
require("wp-config.php");
 
// Connect to WP database
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if ( !$dbc ) {
    die( 'Not Connected: ' . mysql_error());
}
// Select the database
$db = mysql_select_db(DB_NAME);
if (!$db) {
    echo "There is no database: " . $db;
}
 
// Formulate the query
    $query = "
        SELECT post_title,post_content,UNIX_TIMESTAMP(post_date) AS post_date_unix
        FROM `wp_posts`
        WHERE `post_status` = 'publish'
        AND `post_password` = ' '
        AND `post_type` = 'post'
        ORDER BY `wp_posts`.`post_date` DESC
        ";
 
// Perform the query
$result = mysql_query( $query );
 
// Check results of the query and terminate the script if invalid results
if ( !$result ) {
    $message = 'Invalid query.' . PHP_EOL;
    $message .= 'Whole query: ' . $query . PHP_EOL;
    die ( $message );
}
 
/*
 * QUERY RESULT VARIABLES
 */
 
// Count the number of rows returned by the query
$num_rows = mysql_num_rows( $result );
 
// Use the results of query
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
 
// Init var for DATE of the post
$post_date = date( "l, F jS, Y ", $row['post_date_unix'] );  
 
// Init var for TITLE of the post
$post_title = $row['post_title'];
 
// Init var for CONTENT of the post
$post_content = $row['post_content'];
 
// Free the resources associated with the result set
if ( $result ) {
    mysql_free_result( $result );
    mysql_close();
}
?>
The variable to actually post the recent post title is $post_title.

As I said I've looked into this a few days ago and came up with this code I placed below the code above:

Code: Select all

<?php echo strlen($post_title > 50) ? substr($post_title, 0, 50) . '...' : $post_title; ?>
The problem is, for one reason or another it just isn't cutting long blog titles down to 50 char and adding "..." at the end. Can anybody help me fix this code, or suggest a different one?

Many Thanks :)
php4newbies
Forum Newbie
Posts: 3
Joined: Thu Jan 01, 2009 4:38 pm

Re: Need a hand with shortening a text string

Post by php4newbies »

Try:

Code: Select all

<?php echo substr($post_title, 0, 50) . '...'; ?>
mhw
Forum Newbie
Posts: 5
Joined: Thu Jan 01, 2009 2:37 pm

Re: Need a hand with shortening a text string

Post by mhw »

php4newbies wrote:Try:

Code: Select all

<?php echo substr($post_title, 0, 50) . '...'; ?>
That worked a treat.

Thanks ever so much! :)
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Need a hand with shortening a text string

Post by Syntac »

That'll add the "..." even if it's less than 50 chararacters.

Code: Select all

$maxlen = 50;
echo substr($post_title, 0, $maxlen) . ((strlen($post_title) > $maxlen) ? "..." : "");
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Need a hand with shortening a text string

Post by watson516 »

mhw wrote: As I said I've looked into this a few days ago and came up with this code I placed below the code above:

Code: Select all

<?php echo strlen($post_title > 50) ? substr($post_title, 0, 50) . '...' : $post_title; ?>
The problem is, for one reason or another it just isn't cutting long blog titles down to 50 char and adding "..." at the end. Can anybody help me fix this code, or suggest a different one?

Many Thanks :)
If I'm not mistaken, you would just have to move the ) over abit to check. If you just use the substr, it won't check to see if the strlen is more than 50 or not.

Code: Select all

<?php echo strlen($post_title) > 50 ? substr($post_title, 0, 50) . '...' : $post_title; ?>
That should work no? If you include the >50 in the (), strlen would use it all, not just the variable.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Need a hand with shortening a text string

Post by Syntac »

Yeah, it wasn't working because you had your strlen() call messed up.
mhw
Forum Newbie
Posts: 5
Joined: Thu Jan 01, 2009 2:37 pm

Re: Need a hand with shortening a text string

Post by mhw »

Syntac wrote:That'll add the "..." even if it's less than 50 chararacters.

Code: Select all

$maxlen = 50;
echo substr($post_title, 0, $maxlen) . ((strlen($post_title) > $maxlen) ? "..." : "");
That worked absolutely perfectly!

I've replaced the code I was using with this one. Very very pleased.

Thanks ever so much Syntac :mrgreen:
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Need a hand with shortening a text string

Post by Syntac »

You don't have to use my code... Yours would work fine if you fix the strlen() thing.
mhw
Forum Newbie
Posts: 5
Joined: Thu Jan 01, 2009 2:37 pm

Re: Need a hand with shortening a text string

Post by mhw »

The code you supplied does the job nicely, no need to start fixing other codes when the one i've now got works just fine :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Need a hand with shortening a text string

Post by onion2k »

You really ought to be using &hellip; instead of three periods. I'm incredibly fussy about that sort of thing though.
Post Reply