Getting words

Reply

Join Date: Mar 2004
Posts: 94
Reputation: Ragnarok is an unknown quantity at this point 
Solved Threads: 0
Ragnarok Ragnarok is offline Offline
Junior Poster in Training

Getting words

 
0
  #1
Jul 11th, 2004
I have made a news script, but I now want to modify it so that it puts like 75 words insted of 500 characters followed by ..., I just think it looks crap.

I have created the code that should explode all the words by spaces ' ', and them stick that into a for loop that will count to 75 and stick them onto a variable including spaces.

[php]$preview = explode(' ', $news['post']);
$i = 0;
$news['preview'] = '';
for($i = 0; $i > 250; $i++) {
$news['preview'] .= $preview[$i];
if($i != 249) $news['preview'] .= ' ';
}[/php]

for some reason when I output $news['preview'] nothing comes out and I don't know why.

There are to two things I want to know.

1. Why is this simple script not working?
2. Is there a better way of doing this?
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 1,135
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Solved Threads: 6
Team Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Getting words

 
0
  #2
Jul 12th, 2004
Originally Posted by Ragnarok
1. Why is this simple script not working?
For starters, if you want it to loop, switch the > to < in your loop. You're terminating the condition right off the bat, so it's not looping.


Originally Posted by Ragnarok
2. Is there a better way of doing this?
I use this function. Here's the function with an example:

[php]
<?
// Get the first $wordcount words from a sentence - used for long descriptions.
function get_words($str, $wordcount) {
$words=preg_split('/([\s.,;]+)/',$str,$wordcount+1,PREG_SPLIT_DELIM_CAPTURE);
array_pop($words);
return(implode('',$words));
}

$data = "this is a piece of text yeah baby yeah!";
$newdata = get_words($data, 4);
echo $newdata;
?>
[/php]
Check out my blog at http://www.shinylight.com for more stuff about web dev.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 94
Reputation: Ragnarok is an unknown quantity at this point 
Solved Threads: 0
Ragnarok Ragnarok is offline Offline
Junior Poster in Training

Re: Getting words

 
0
  #3
Jul 12th, 2004
lol woops, thanx a lot for your help. It always useful to have someone else look at your code.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 94
Reputation: Ragnarok is an unknown quantity at this point 
Solved Threads: 0
Ragnarok Ragnarok is offline Offline
Junior Poster in Training

Re: Getting words

 
0
  #4
Jul 12th, 2004
I have however modified the function so that if it has less than the designated words it will not do the array_pop
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 1,135
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Solved Threads: 6
Team Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Getting words

 
0
  #5
Jul 13th, 2004
Cool. Glad it's working for ya.
Check out my blog at http://www.shinylight.com for more stuff about web dev.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3156 | Replies: 4
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC