Page 1 of 1

How can I extract full words from a long string sentence

Posted: Tue Oct 11, 2005 2:13 pm
by Antek_Ryu
Hi All

I am currently extracting information from a database which is a string. What I need to do is extract a few words from the sentence. I currently have 26 characters I can extract from the string that are allowed to go in a html table (first row) then I must put in a html break (<br>) and insert the second 26 characters on a second row from where I left off on the first line. The problem with this is that words get cut off I want whole words to go in the first and second lines. How can I do this?

Currently I am using the code

Code: Select all

$news = "Hello this is a long string item that i am inserting in a web page"

for the first line of text
substr($news, 0, 26) 

I break (<br>)

for the second line of text
substr($news1, 26, 52)

RESULT which i dont want as word get cut

Hello this is a long strin
g item that i am inserting





Many thanks php gurus
Antek

Posted: Tue Oct 11, 2005 2:22 pm
by feyd
Useful Posts wrote:Chopping of text without losing words: chopping of text

Posted: Tue Oct 11, 2005 5:50 pm
by pickle
Easiest way I've found is to use strtok(). It's a bit weird, but works like a charm.

strtok(): http://www.php.net/strtok

Posted: Tue Oct 11, 2005 6:19 pm
by Skara
I think I've seen a function that does this for you. You just specify where to wrap and it won't wrap on a word. I don't remember the name of it, though. ^^; *looks*
Ah, wordwrap.