Page 1 of 1

Linewrapping?

Posted: Sat May 13, 2006 2:31 am
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?

Posted: Sat May 13, 2006 3:33 am
by s.dot
Have a look at substr() or the word breaking post in the useful posts topic.

Re: Linewrapping?

Posted: Sat May 13, 2006 3:45 am
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

Posted: Sat May 13, 2006 3:51 am
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

Posted: Sat May 13, 2006 4:06 am
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

Posted: Sat May 13, 2006 4:09 am
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