Need a quick hand cutting some text off.

Discussion in 'PHP' started by PixelRaider, Dec 29, 2008.

  1. #1
    Hey guys,

    I'm working on a fairly simple site, a directory showing different blogs...their post counts, their article counts and recent posts all appear on my homepage.

    This is the code I am using to call the recent comment:
    <?php
    require_once('wp-blog-header.php');
    $count_posts = wp_count_posts();
    $published_posts = $count_posts->publish;
    ?>
    
    <?php  query_posts('showposts=1'); ?>
        <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a>
            <?php endwhile; ?>
            <?php else : ?>
    <?php endif; ?>
    PHP:
    That is in a file called recent-article.php.

    I then use a php include to display recent-article.php on my website. The problem is the space the recent title needs to fit in is only about 150px wide, and if there is a really long blog title it will jump out of the little box it shows in. So here's my question...

    Is there a way to shorten down / cut off the end of the text?

    For example, how could I turn:

    "10 ways to get seen in Google, Yahoo and Ask"


    to

    "10 ways to..."


    Thanks guys. :eek:
     
    PixelRaider, Dec 29, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    www.php.net/substr

    echo strlen($text > 125) ? substr($text, 0, 125) . '...' : $text;
    PHP:
    Good enough?
     
    Danltn, Dec 29, 2008 IP