How can I extract full words from a long string sentence

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
Antek_Ryu
Forum Commoner
Posts: 34
Joined: Tue Aug 09, 2005 10:55 am

How can I extract full words from a long string sentence

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Useful Posts wrote:Chopping of text without losing words: chopping of text
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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.
Post Reply