... after completion of a word

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
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

... after completion of a word

Post by kkonline »

Hi there, I am working on a search display part.
I have used

Code: Select all

if (strlen($rows['content'])>100)
					$rows['content'] = substr($rows['content'],0,100)."...";;
					echo $rows['content'] . '<br>';
to restrict the display of more than 100 characters for displaying a short content.

The problem I am facing is that sometimes the words are cut in between as shown below

"Redistributions of source code must retain the a..."

I want a... to be a complete and not to break the work in middle

it should be "Redistributions of source code must retain the above..."

How to let the complete word show and then display ...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There is a thread about this topic found in Useful Posts.
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

hi feyd i used

Code: Select all

preg_match('#^\s*(.{21,}?)\s+.*$#s', $rows['content'], $match);
                echo ($match[1]) . '<br>';
Actually i am using it inside a while loop and think that is causing the problem .

In displaying the search results suppose there are 5 results then 3 of then are displayed with properly chopped data and the next are displayed as "Notice: Undefined offset: 1 in d:\easyphp\www\searchtag.php on line 75"

where line 75 is preg_match statement
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There was no match; the string was shorter than the specification.
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 »

wordwrap() might be useful
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

This sort of thing came up recently and was solved at the database layer:

http://dev.mysql.com/doc/refman/5.0/en/ ... ring-index
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Post by mrkite »

pickle wrote:wordwrap() might be useful
Man, php has everything!

I myself have done this by cutting the string, and then looping backwards until I found a space. Nice to know there's a function.

PHP always surprises me with its function library. I can't tell you how many times I wrote a regex to strip out html tags before I discovered strip_tags()
Post Reply