Page 1 of 1

... after completion of a word

Posted: Fri Oct 26, 2007 1:07 am
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 ...

Posted: Fri Oct 26, 2007 1:11 am
by feyd
There is a thread about this topic found in Useful Posts.

Posted: Fri Oct 26, 2007 3:17 am
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

Posted: Fri Oct 26, 2007 3:24 am
by feyd
There was no match; the string was shorter than the specification.

Posted: Fri Oct 26, 2007 10:51 am
by pickle
wordwrap() might be useful

Posted: Sat Oct 27, 2007 12:15 am
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

Posted: Sat Oct 27, 2007 12:51 am
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()