Hey, I have been given the task of creating the website for our school newspaper. Im almost done and about ready to add content to the site. So far, ive been doing the php myself but I am very new at it and all i really know how to do is put stuff in and take stuff out of databases. My friend has also writen a bit of the code.
The problem that I am having is that on a page, php code will list all the articles from the current newspaper issue (about 10 articles) with the Title, Author(s), and Story Text ($TITLE, $AUTHOR1 $AUTHOR2, $STORYBODY). For the story body however, only the first 75 words will be displayed, because it is a preview. The user can click the title to see the full story.
The code that my friend wrote is supposed to only list the first 75 words of a story from $STORYBODY. He says it counts the spaces until it reaches 75. However, when I put the code on my page it only lists the very first word, nothing else.
Here is the code:
<?
$offset = 0;
for ($x=0; $x < 75; $x++) {
$temp = strpos($STORYBODY,' ',$offset);
if ($temp == false) break;
$pos = $temp;
$new_body = substr($STORYBODY,0,$pos);
}
echo $new_body;
?>
Can anyone see the problem? Thanks!yeah I see wha tis wrong. why have it in a loop?
$new_body = substr($STORYBODY,0,75)."...";
tht code you have is all wrong. you don't have to loop.Try this:
<?php
$offset = 0;
for ($x = 0; $x < 75; $x++) {
$temp = strpos($STORYBODY, ' ', $offset+1);
if ($temp === false) {
break;
} else {
$offset = $temp;
}
}
$new_body = substr($STORYBODY,0,$offset).'...';
echo $new_body;
?>but why? strpos automatically counts spaces.
unless of course you want 75 spaces instead of chars. which doesn't make since.Note that he wants the first 75 words, not characters...man I need to get my eyes checked. I thought you had substr where you have strpos at.
what if the user enters a lot of spaces at the end of the summary? will it bomb that script?Originally posted by scoutt
what if the user enters a lot of spaces at the end of the summary? will it bomb that script?
Bomb.. Nah, but I guess it will have less than 75 words though. It will just show up as one space in the browser anyway... And if there were to be lots of spaces for some reason, they must either stop doing that(really), or make the script smarter.. But I think this will do for now thx guys, the code works
The problem that I am having is that on a page, php code will list all the articles from the current newspaper issue (about 10 articles) with the Title, Author(s), and Story Text ($TITLE, $AUTHOR1 $AUTHOR2, $STORYBODY). For the story body however, only the first 75 words will be displayed, because it is a preview. The user can click the title to see the full story.
The code that my friend wrote is supposed to only list the first 75 words of a story from $STORYBODY. He says it counts the spaces until it reaches 75. However, when I put the code on my page it only lists the very first word, nothing else.
Here is the code:
<?
$offset = 0;
for ($x=0; $x < 75; $x++) {
$temp = strpos($STORYBODY,' ',$offset);
if ($temp == false) break;
$pos = $temp;
$new_body = substr($STORYBODY,0,$pos);
}
echo $new_body;
?>
Can anyone see the problem? Thanks!yeah I see wha tis wrong. why have it in a loop?
$new_body = substr($STORYBODY,0,75)."...";
tht code you have is all wrong. you don't have to loop.Try this:
<?php
$offset = 0;
for ($x = 0; $x < 75; $x++) {
$temp = strpos($STORYBODY, ' ', $offset+1);
if ($temp === false) {
break;
} else {
$offset = $temp;
}
}
$new_body = substr($STORYBODY,0,$offset).'...';
echo $new_body;
?>but why? strpos automatically counts spaces.
unless of course you want 75 spaces instead of chars. which doesn't make since.Note that he wants the first 75 words, not characters...man I need to get my eyes checked. I thought you had substr where you have strpos at.
what if the user enters a lot of spaces at the end of the summary? will it bomb that script?Originally posted by scoutt
what if the user enters a lot of spaces at the end of the summary? will it bomb that script?
Bomb.. Nah, but I guess it will have less than 75 words though. It will just show up as one space in the browser anyway... And if there were to be lots of spaces for some reason, they must either stop doing that(really), or make the script smarter.. But I think this will do for now thx guys, the code works