SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast PlayStone's Avatar
    Join Date
    Jan 2004
    Location
    Canada
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic text wrapping.

    Hi,

    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.

    This is
    what I have right now.

    PHP Code:
    $wrap_description wordwrap(substr($site_description,0,115)." ..."64"<br>"); 

    Any ideas?
    Thanks.

  2. #2
    SitePoint Addict Ramiro S's Avatar
    Join Date
    May 2003
    Posts
    321
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
    $query="SELECT field1, field2, SUBSTRING_INDEX(varcharfield,' ',70) as smalltext......" 
    That will retrieve the first x (70 in this example) words.
    Quasar - Web Development - Free Avatars

  3. #3
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    $descript = wordwrap($site_description,75,"<br>";
    $wrap_description = $descript." ...";
    Also have a look at wordwrap in the manual. You're parameters are setup incorrectly for that.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  4. #4
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Southwest US
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Thanks,
    Itshim


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •