Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: word limit

  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question word limit

    Hi all,

    got this code, which am currently using on my input form cuts the description down to 20 characters, but what am looking for now is a string which cuts off at the end of a sentence. For example:

    The Buyers/Prospects have responded to advertisements in the National Press, Magazines and via inserts. Approximately 35% of customers pay by credit or debit card with Multi-buyers being very high.

    to show:

    The Buyers/Prospects have responded to advertisements in the National Press, Magazines and via inserts.

    <?php
    $string = $row_rsLists['listdescription'];
    print wordlimit($string, 20);
    ?>

    Does anyone have any ideas?

    Feel free to use the script above.

    Many thanks

    Maria

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just the first sentence you want? Try this:
    PHP Code:
    <?php
    function return_first_sentence($str) {
        
    $length strlen($str);
        for(
    $i=0$i $length$i++) {
            if(
    substr($str$i1) == '.') {
                return 
    substr($str0$i+1);
            }
        }
        return 
    $str;
    }

    $string 'The Buyers/Prospects have responded to advertisements in the National Press, Magazines and via inserts. Approximately 35% of customers pay by credit or debit card with Multi-buyers being very high.';
    print 
    return_first_sentence($string);
    ?>

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by delinear
    Just the first sentence you want? Try this:
    PHP Code:
    <?php
    function return_first_sentence($str) {
        
    $length strlen($str);
        for(
    $i=0$i $length$i++) {
            if(
    substr($str$i1) == '.') {
                return 
    substr($str0$i+1);
            }
        }
        return 
    $str;
    }

    $string 'The Buyers/Prospects have responded to advertisements in the National Press, Magazines and via inserts. Approximately 35% of customers pay by credit or debit card with Multi-buyers being very high.';
    print 
    return_first_sentence($string);
    ?>
    that's a nice bit of string, however each category displays a different sentence and therefore cannot just specify one individual sentence. Is it possible to have a string which looks at the description and cuts off at the end of the first sentence?

    <?php echo $row_rsPromo['listdescription']; ?> this is the insert from my database, which shows the description on the page.

    hope that makes sense.

    Many thanks

    Maria

  • #4
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    If it is always going to be the first sentence you want to extract then this does it very simply.
    Code:
    <?php
    $string = 'The Buyers/Prospects have responded to advertisements in the National Press, Magazines and via inserts. Approximately 35% of customers pay by credit or debit card with Multi-buyers being very high.';
    
    $shortString = explode (".", $string);
    echo $shortString[0].".";
    ?>

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <?php echo return_first_sentence($row_rsPromo['listdescription']); ?>

  • #6
    New Coder
    Join Date
    May 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you know what I've figured it out amazingly! geez it really feels good when you do it yourself!

    <?php function return_first_sentence($str) { $length = strlen($str);
    for($i=0; $i < $length; $i++) {
    if(substr($str, $i, 1) == '.') {
    return substr($str, 0, $i+1);
    }
    }
    return $str;
    }

    $string = $row_rsPromo['listdescription'];
    print return_first_sentence($string); ?>

    something like that - however a couple of problems - want to return two sentences, so it ends at the second sentence, thinking just change

    <?php function return_first_sentence($str) to
    <?php function return_second_sentence($str) not to sure if works though.

    also get this message:
    Fatal error: Cannot redeclare return_first_sentence() (previously declared in c:\web\jpart\promo.php:164) in c:\web\jpart\promo.php on line 164

    as its a repeat region

    ???

    Maria

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, change the function to this:

    PHP Code:
    function return_first_sentence($str$num=1) { 
        
    $length strlen($str); 
        
    $inc=1;
        for(
    $i=0$i $length$i++) { 
            if(
    substr($str$i1) == '.') { 
                if(
    $inc==$num) {
                    return 
    substr($str0$i+1); 
                }
                            
    $inc++;
            } 
        } 
        return 
    $str

    Now you can call this by either using:
    PHP Code:
    <?php echo return_first_sentence($row_rsPromo['listdescription']); ?>
    This will return the default one message. Alternatively you can specify exactly how many sentences you want to call using the following format:
    PHP Code:
    //For two sentences:
    <?php echo return_first_sentence($row_rsPromo['listdescription'], 2); ?>

    //For five sentences:
    <?php echo return_first_sentence($row_rsPromo['listdescription'], 5); ?>

    //etc
    Hope this helps

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could you keep it from truncating sentences like, "I just spend $4.97 on a 7.5 ounce latte." ?

  • #9
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Harry Armadillo
    Could you keep it from truncating sentences like, "I just spend $4.97 on a 7.5 ounce latte." ?
    Hmm, not so straightforward. Regular expressions might be better although a quick and dirty way would be to just split the chunk where it encounters a period followed by a space ". " - still not ideal but slightly better.

    PHP Code:
     function return_first_sentence($str$num=1) {  
        
    $length strlen($str);  
        
    $inc=1
        for(
    $i=0$i $length$i++) {  
            if(
    substr($str$i2) == '. ') {  
                if(
    $inc==$num) { 
                    return 
    substr($str0$i+1);  
                } 
                            
    $inc++; 
            }  
        }  
        return 
    $str;  

    I think that should do it, though I might attempt some form of regex if no-one else fancies it

  • #10
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It looks like you're pulling this sentence from a db. If so, you can pull a pre-truncated version through your query:

    Code:
    SELECT 
     LEFT( fieldname,LOCATE('. ',fieldname,x) ) AS truncated
    FROM
     table
    Where x is approximately the length of the string you want returned. That will find the first sentence-ending period after x and return the string up to that point.

    Of course, this assumes your sentences will all end with a period. As has been mentioned above, a regex is probably your most bulletproof option. I prefer to dodge the whole issue, and just truncate between words and add an ellipses, if possible:

    Code:
    SELECT
     CONCAT( LEFT( fieldname,LOCATE(' ',fieldname,x) ), '...') AS truncated
    FROM
     table


  •  

    Posting Permissions

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