2 PHP questions

Soldato
Joined
18 Oct 2002
Posts
7,639
Location
Sutton Coldfield, Birmingham
I'm working on a php news script but am having trouble coding a few bits of it, the first thing is..

Say a news post has 1000 words, this will be too many to show on the front page so I want to give the user the ability to add a tag which will split the news post into two sections, allowing me to display the first section on the front page and then the reader can click a 'read more' link to view the rest. How can I do this?

Secondly, also on the front page I want some small news boxes which show the latest news headlines aswell as the first 20 words of each news post, I don't want the user to have to use a tag for this. I'd like PHP to select the first 20 words and print them to the screen, then stop. I assume you have to use arrays and strings for this, can someone give me some advice?

Thanks :)
 
use substr, strpos and strrpos.

An example:
Code:
$chopped = substr($newsArticle, 0, 300);
$brief = substr($chopped, 0, strrpos($chopped, ' '));
echo $brief. ' ...';
will display the characters up to the closest space before the 300 limit.
 
http://dfhaii.com/source/wordTest.php (source is there) does exactly what you want. The problem with the solution above is that it doesn't split by number of words, but by characters instead (which is arguably a better method as it'll have a more consistant size), and as such didn't meet your brief.
 
Look at the php function 'substr'. You can use this to select the first x characters from the string, alternatively you could 'split' (another php function) the string on whitespaces to return an array of words, you could then just spit out the first 20 values of the array.

Not sure how you're going to do the first thing though. What do you mean by a 'tag'?

[Edit]People got there first :p[/Edit]
 
Look at the php function 'substr'. You can use this to select the first x characters from the string, alternatively you could 'split' (another php function) the string on whitespaces to return an array of words, you could then just spit out the first 20 values of the array.

Not sure how you're going to do the first thing though. What do you mean by a 'tag'?

[Edit]People got there first :p[/Edit]

I think he means some sort of unique marker that he can use to identify a manual summary.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi id mi. In hac habitasse platea dictumst. Praesent vel felis sit amet neque faucibus lacinia. Donec pede massa, tempus sed, egestas eget, volutpat at, magna. Praesent hendrerit ultricies mi. Pellentesque dui. Aliquam erat volutpat. Nullam imperdiet tortor sit amet tellus. ///SPLIT///Duis in odio et sapien posuere fringilla. Suspendisse eleifend leo ut enim. Duis auctor, nunc in pharetra adipiscing, metus elit venenatis erat, convallis posuere leo purus eget magna. Nam varius. Mauris in nisl. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi adipiscing. Aliquam vulputate, enim et bibendum volutpat, libero metus dictum dui, vel consectetuer urna erat et metus. Cras ornare neque sed justo.
If this is the case then he'll need something like this:
Code:
$summary = substr($article, 0, strpos($article, '///SPLIT///'));
$article = str_replace('///SPLIT///', '', $article);
You could improve upon this by using any section of the article as the summary by having START and END markers instead of SPLIT.
 
Back
Top Bottom