Dynamic text wrapping.
I am trying to do a dynamic text wrapping feature for my search results.
My "site_description" field is a VARCHAR in the DB with 200 chars.
What I need to have is to lines printed with no more than 70 chars per line and at the end put "...", like Google does.
The problem I am having is that my word wrap is cutting a word in half on the second line. So, I need to make this dynamic so that the second line always has complete words, this will mean that the two lines will not be the same lenght but it's ok.
what I have right now.
$wrap_description = wordwrap(substr($site_description,0,115)." ...", 64, "<br>");
str_word_count() funciton ca do that for you... as far as I remember you can retrieve the word count or an array of the words with the position of that word... then use strpos and str_replace to add the "... more stuff".
If you want to ddo it from the query you can use something like this:
That will retrieve the first x (70 in this example) words.
$query="SELECT field1, field2, SUBSTRING_INDEX(varcharfield,' ',70) as smalltext......"
Also have a look at wordwrap in the manual. You're parameters are setup incorrectly for that.
$descript = wordwrap($site_description,75,"<br>";
$wrap_description = $descript." ...";
Are you sure it is wordwrap() which is cutting the word in half? The optional third parameter wordwrap() accepts is to force wordwrap() to cut words in half, if that is not set, it should choose the word which ends before the 64th character (from your example).
I think the words are getting sliced in half by the use of substr().
I hope I didn't misunderstand you post.