Linewrapping?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
duzchip
Forum Newbie
Posts: 3
Joined: Sat May 13, 2006 2:27 am

Linewrapping?

Post by duzchip »

Hi, i'm writing a site with some diffrent features. On the startpage the news are published but now i need some help. I only want the first half line or so of the news published with a few dots after and when they click on that text (being a link) they can read the entire news. I know how to write all that except the wrapping. Someone help please?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Have a look at substr() or the word breaking post in the useful posts topic.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: Linewrapping?

Post by aerodromoi »

duzchip wrote:Hi, i'm writing a site with some diffrent features. On the startpage the news are published but now i need some help. I only want the first half line or so of the news published with a few dots after and when they click on that text (being a link) they can read the entire news. I know how to write all that except the wrapping. Someone help please?
You could always use

Code: Select all

explode(" ", $string);
to create an array containing the words of the entry. Take the first 6 or seven elements in this array (using a loop) and add "..." and you're done. If you're planning on featuring news taking up several pages, I'd use substr() first.

aerodromoi
duzchip
Forum Newbie
Posts: 3
Joined: Sat May 13, 2006 2:27 am

Post by duzchip »

I'm sorry to be a pain in the ** now but could someone please just write a little example. I'm very tired and have a huge hangover so i'm not at the peak of my mind right now. :P
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Then go to bed and have a good sleep. You'll be more productive afterwards.. (And we don't have to waste our time giving examples that are already available in the manual...)

In case you're getting the text from a database it's a waste to fetch the complete text if you only want the first 50 words or so. In that case you can use a query like:

Code: Select all

SELECT SUBSTRING_INDEX(body,‘ ‘,50) AS introduction FROM mytable
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

duzchip wrote:I'm sorry to be a pain in the ** now but could someone please just write a little example. I'm very tired and have a huge hangover so i'm not at the peak of my mind right now. :P
Here you go:

Code: Select all

<?php
$string = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla diam.";
$wordarray = explode(" ", $string);
$numw = 6;
$outputstring = "";

for ($i=0;$i<$numw;$i++){
  $outputstring .= $wordarray[$i]." ";
}
$outputstring .= "...";

echo $outputstring;
?>
btw: I just had two secs on my hands - but stating a hangover as the reason for asking for an example - that's audacious...

aerodromoi
Post Reply