943,910 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3468
  • PHP RSS
Jul 11th, 2004
0

Getting words

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Ragnarok is offline Offline
94 posts
since Mar 2004
Jul 12th, 2004
0

Re: Getting words

Quote 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.


Quote 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]
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Jul 12th, 2004
0

Re: Getting words

lol woops, thanx a lot for your help. It always useful to have someone else look at your code.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Ragnarok is offline Offline
94 posts
since Mar 2004
Jul 12th, 2004
0

Re: Getting words

I have however modified the function so that if it has less than the designated words it will not do the array_pop
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Ragnarok is offline Offline
94 posts
since Mar 2004
Jul 13th, 2004
0

Re: Getting words

Cool. Glad it's working for ya.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP Pictures Layout
Next Thread in PHP Forum Timeline: linking issues





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC