Results 1 to 5 of 5

Thread: How can I specify the length of the string more precisely?

  1. #1
    Join Date
    Oct 2012
    Posts
    157
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default How can I specify the length of the string more precisely?

    I am using this to shorten long titles:

    Code:
    <?php
    
    $title = $row['adtitle'];
    if (strlen($title) > 50) {
    	$title = substr($title, 0, 50).'...';
    }
    echo $title;
    
    ?>
    The thing is it shortens them this way:

    Selling housing in OH. Stop by to check it ou...

    Make money working from home. Best opport...


    I want it to shorten them right before the last word, so that it would look like this:

    Selling housing in OH. Stop by to check it...

    Make money working from home. Best...


    Any ideas how to do that?

    Thanks!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    First of all, of the examples in your post, I would rather see the ones with the partial words, they're more informative.

    But to answer the question, you could use a regular expression and preg_replace, or just look for the last index (strrpos) of space.

    Whichever you do, I think it would have to be an at least two step operation. First you would shorten the string to 50 characters as you already do, next you would find the spot nearest the end where you want to actually chop it at and add the ...
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2012
    Posts
    157
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default

    This seems to be working great:

    Code:
    <?php
    
    $title = $row['adtitle'];
    if (strlen($title) > 50) {
        $title = preg_replace('~\s+\S+$~', '', $title);
        $title = $title."...";
    }
    echo $title;
    
    ?>
    Is that what you had in mind?

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    That would just chop off the last word, regardless of how long the string is. You'd need to chop it at 50 characters first.

    PHP Code:
    <?php

    $title 
    $row['adtitle'];
    if (
    strlen($title) > 50) {
        
    $title substr$title,0,50 );
        
    $title preg_replace('~\s+\S+$~'''$title);
        
    $title $title."...";
    }
    echo 
    $title;

  5. The Following User Says Thank You to traq For This Useful Post:

    qwikad.com (10-24-2013)

  6. #5
    Join Date
    Oct 2012
    Posts
    157
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by traq View Post
    that would just chop off the last word, regardless of how long the string is. You'd need to chop it at 50 characters first.

    PHP Code:
    <?php

    $title 
    $row['adtitle'];
    if (
    strlen($title) > 50) {
        
    $title substr$title,0,50 );
        
    $title preg_replace('~\s+\S+$~'''$title);
        
    $title $title."...";
    }
    echo 
    $title;
    perfect!
    Last edited by traq; 10-25-2013 at 01:24 AM.

Similar Threads

  1. How to precisely link to content of SSI script II -iframe?
    By wol4 in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 07-10-2012, 10:17 PM
  2. Append string to the end of string via a loop
    By l_kris06 in forum JavaScript
    Replies: 1
    Last Post: 06-24-2009, 02:59 PM
  3. Resolved parseInt() is still recognizing string as string only
    By JShor in forum JavaScript
    Replies: 3
    Last Post: 06-21-2009, 12:14 AM
  4. Replies: 2
    Last Post: 11-05-2008, 05:40 PM
  5. Access no error is string length > 255
    By jayapalchandran in forum ASP
    Replies: 2
    Last Post: 07-08-2008, 01:39 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •