| | |
Getting words
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Mar 2004
Posts: 94
Reputation:
Solved Threads: 0
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?
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?
•
•
•
•
Originally Posted by Ragnarok
1. Why is this simple script not working?
•
•
•
•
Originally Posted by Ragnarok
2. Is there a better way of doing this?
[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.
Cool. Glad it's working for ya.
Check out my blog at http://www.shinylight.com for more stuff about web dev.
![]() |
Similar Threads
- Simple Banned Words Filter (PHP)
- Create Function that prints letters, words and sentences (C++)
- Need Help with the MROI Program for AD Words (Pay-Per-Click Advertising)
- Delete words error!! (C++)
Other Threads in the PHP Forum
- Previous Thread: PHP Pictures Layout
- Next Thread: linking issues
Views: 3156 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class clean cms code countingeverycharactersfromastring cron curl database date directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail match menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions simple sms soap source space spam speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube






