Current location: Hot Scripts Forums » Programming Languages » PHP » need a systex to show first 50 words from a variable


need a systex to show first 50 words from a variable

Reply
  #1 (permalink)  
Old 03-22-05, 04:21 AM
loeddie loeddie is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
need a systex to show first 50 words from a variable

Dear all,

Let's say I have a variable $pro_description, it was a long text description, but I only want to show first 50 words, how can I do it? thanks for any help.

Best Regards
Eddie Lo
PHP Newbie
Reply With Quote
  #2 (permalink)  
Old 03-22-05, 04:51 AM
Orbitz Orbitz is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

//suppose:

$pro_description "this text contains up to 50 characters ......";

preg_match('#^\s*(.{50,}?)\s+.*$#s'$pro_description$short_description);

echo 
$short_description[0]; 
// if there is no space right after the 50th character, it continues matching until it hits white space; this way, it won't cut off word and makes it meaningless.
Reply With Quote
  #3 (permalink)  
Old 03-22-05, 08:33 AM
loeddie loeddie is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
I just follow the method that taught by Orbitz, but I got the seem result, all the data from the record from database shown instead of only show the first 50 words, any help will be appreciate, thanks!

Eddie
Reply With Quote
  #4 (permalink)  
Old 03-22-05, 08:37 AM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Orbitz
PHP Code:

//suppose:

$pro_description "this text contains up to 50 characters ......";

preg_match('#^\s*(.{50,}?)\s+.*$#s'$pro_description$short_description);

echo 
$short_description[0]; 
// if there is no space right after the 50th character, it continues matching until it hits white space; this way, it won't cut off word and makes it meaningless.
won't work as you wish. here, you're capturing whole text (from the statement "echo $short_description[0];"). the regexp pattern will also fail to grab 50 characters because you didn't set the ungreedy flag (you set the first subpattern to fetch anything. here, space and tab is included).

since he wants to line up 50 words and not 50 chars from the beginning, a simple (not optimized) solution could be:
PHP Code:

function get_n_words($longtext,$n) {

    
$first_n preg_split('/[\s]+/',$longtext,$n,PREG_SPLIT_NO_EMPTY);
    
$first_n_count count($first_n);
    
$returned '';
    for(
$i=0$i<$n$i++) {
        if(
$i>=$first_n_count) break;
        
$returned .= $first_n[$i].' ';
    }
    return 
$returned;
}

$pro_description 'Consider this a very long long text. More than 50 words';
//Print first 50 words if available or less
print get_n_words($pro_description,50); 
regards,
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #5 (permalink)  
Old 03-22-05, 08:43 AM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
oops... seemed my post was too late..
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #6 (permalink)  
Old 03-22-05, 08:59 AM
loeddie loeddie is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
I just tested moronovich 's code and find that it is not working, please help me on that, thanks a million.

Here is what I pasted my code, what it still print the whole thing at $pro_description.

function get_n_words($longtext,$n) {
$first_n = preg_split('/[\s]+/',$longtext,$n,PREG_SPLIT_NO_EMPTY);
$first_n_count = count($first_n);
$returned = '';
for($i=0; $i<$n; $i++) {
if($i>=$first_n_count) break;
$returned .= $first_n[$i].' ';
}
return $returned;
}

$pro_description = 'Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words Consider this a very long long text. More than 50 words ';
//Print first 50 words if available or less
print get_n_words($pro_description,50);
Reply With Quote
  #7 (permalink)  
Old 03-22-05, 09:16 AM
bizzar528's Avatar
bizzar528 bizzar528 is offline
Community Liaison
 
Join Date: Sep 2004
Location: Pennsylvania, US
Posts: 1,550
Thanks: 2
Thanked 16 Times in 15 Posts
Here's my code for something similar that I do for one of my scripts.

PHP Code:

$truncate 50;

$first50 substr(nl2br($fullstring),0,$truncate);
echo 
$first50
That should be easily converted into a function.
__________________
Yep, it's a signature...
Reply With Quote
  #8 (permalink)  
Old 03-22-05, 09:32 AM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
sorry. i didn't test it myself, but i just found an interesting fact. it seems that preg_split function is buggy (i don't know whether this is valid assumption, but i'll check the source later to make sure about this).
you can hack above code to work, by changing the function into:
PHP Code:

function get_n_words($longtext,$n) {

    
$first_n preg_split('/[\s]+/',$longtext,$n,PREG_SPLIT_NO_EMPTY);
    
$first_n_count count($first_n);
    
$returned '';
    for(
$i=0$i<$n$i++) {
        if(
$i == $n 1) {
            
$returned .= preg_replace('/([^\s]+).*$/',"\\1",$first_n[$i]);
        }
        else {
            
$returned .= $first_n[$i].' ';
        }
        if(
$i>=$first_n_count) break;
    }
    return 
$returned;

good luck!
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #9 (permalink)  
Old 03-22-05, 09:51 AM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by bizzar528
Here's my code for something similar that I do for one of my scripts.

PHP Code:

$truncate 50;

$first50 substr(nl2br($fullstring),0,$truncate);
echo 
$first50
That should be easily converted into a function.
notes on above code:
1. it cuts up to 50 chars from the beginning and not words.
2.there is always a chance for the output to not be displayed properly since nl2br function is executed before the the substr (imagine if i have <br /> with offset 48).

any comments?
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #10 (permalink)  
Old 03-22-05, 10:51 AM
loeddie loeddie is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by moronovich
sorry. i didn't test it myself, but i just found an interesting fact. it seems that preg_split function is buggy (i don't know whether this is valid assumption, but i'll check the source later to make sure about this).
you can hack above code to work, by changing the function into:
PHP Code:

function get_n_words($longtext,$n) {

    
$first_n preg_split('/[\s]+/',$longtext,$n,PREG_SPLIT_NO_EMPTY);
    
$first_n_count count($first_n);
    
$returned '';
    for(
$i=0$i<$n$i++) {
        if(
$i == $n 1) {
            
$returned .= preg_replace('/([^\s]+).*$/',"\\1",$first_n[$i]);
        }
        else {
            
$returned .= $first_n[$i].' ';
        }
        if(
$i>=$first_n_count) break;
    }
    return 
$returned;

good luck!
This code works great, but when I try to loop my recordset, the function only work for the first record, the second and the rest will not run the function at all, am I do anything wrong here? Sorry for a lot of questions..>"<

regards
Eddie
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help! urgent.. how to select next 50 records? angela ASP 3 04-24-04 12:58 PM


All times are GMT -5. The time now is 05:38 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.