Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Location
    the windy city
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts

    displaying first x characters, but not cutting in the middle of the word

    I want to display the forst x characters of a db text field, which I guess I can do using a substring. But I don't want it to cut off in the middle of the word. How would I accomplish that?

  • #2
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Hmm, I know I've seen this here before.

    How about something like...

    PHP Code:
    $text preg_match_all("/(.{1,50}\s.{1,15})?/",$dataBaseText,$matches);
    print 
    $matches[0]; 

    Not tested, but something similar should work.

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes. Has been posted here before. I use this

    PHP Code:
    /*jesus at home dot com
    03-Apr-2003 08:53 
    How to make a nice trim of text without break words in the middle :*/

     
    $maxTextLenght=125;
      
    $aspace=" ";
      if(
    strlen($text) > $maxTextLenght ) {
         
    $text substr(trim($text),0,$maxTextLenght); 
         
    $text substr($text,0,strlen($text)-strpos(strrev($text),$aspace));
         
    $text $text.'...';
      } 
    So you cut of, the run a reverse search for a space and do a substr with that is number of characters.

    Of course, when i pull it from a db, i do the initial chopping of with sql
    PHP Code:
    sql="select left(variable, 125)  as variable from table";
    ...
    $aspace=" ";
    if(
    strlen($text) > $maxTextLenght ) {
         
    $text substr($text,0,strlen($text)-strpos(strrev($text),$aspace));
         
    $text $text.'...';

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #4
    Regular Coder
    Join Date
    Dec 2002
    Location
    the windy city
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by raf
    Yes. Has been posted here before.
    I did a search, so I saw that. But i got confused, so figured I'd ask again.

    Quote Originally Posted by raf
    PHP Code:
    sql="select left(variable, 125)  as variable from table"
    can you please explain what "variable" should stand for in this line?

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    inside the left-function, this needs bto be the name of the column /control /variable inside your table. The column you wan't to pulle the value from. The second variable is then an alias (could be the same name as the original columname. I suppose you'll kknow that 'table' needs to be replaced by the tablename.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #6
    Regular Coder
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Don't forget about the awesome concatenation operator! Kind of a silly revision but you could modify the last line like this:

    from:
    PHP Code:
    $text $text.'...'
    to:
    PHP Code:
    $text .= '...'
    Makes no difference....I just like small code so I try to squeez out extra stuff wherever I can.


  •  

    Posting Permissions

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