SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard
    Join Date
    May 2002
    Posts
    1,370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    susbtr echo - only on spaces?

    Wither the susbtr, is there a way to have the echo end while only on spaces?
    - So partial words don't get chopped?

    <?php
    $abbrev = substr($row_results['ML'], 0, 110);
    $abbrev .= "...";
    echo $abbrev; ?>

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Not sure I understand, you want to take what and turn it into what?

  3. #3
    SitePoint Wizard
    Join Date
    May 2002
    Posts
    1,370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As it is, this is ending off at mid-word.

    EX: "Sitepoint" could become "Sit" due to the substr character count.

    Wanting to end off when there is the nearest space between words, if this is possible.

  4. #4
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You can use a regular expression such as '/^\s*([^\s]+)/' to achieve what you want.

  5. #5
    SitePoint Wizard
    Join Date
    May 2002
    Posts
    1,370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where/how/could that go within ?

    <?php
    $abbrev = substr($row_results['ML'], 0, 110);
    $abbrev .= "...";
    echo $abbrev; ?>

  6. #6
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I still need more information on exactly what $abbrev is meant to achieve. What is the exact return of $row_results['ML']? (btw, I'm going to sleep now, so it won't be till the morning that I answer).

  7. #7
    SitePoint Wizard
    Join Date
    May 2002
    Posts
    1,370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_fuel
    Hi,

    I still need more information on exactly what $abbrev is meant to achieve. What is the exact return of $row_results['ML']? (btw, I'm going to sleep now, so it won't be till the morning that I answer).
    $abbrev limits the echo to exactly 110 characters.

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use wordwrap() combined with some other string functions to achieve what you need, but i think its easier to use regex.

    PHP Code:
    preg_match('#^.{1,110}\s#s'ltrim($text), $matches);
    echo 
    $matches[0]; 
    this will give you a string 110 chars or less in length. it will cut the string on the last whitespace character within the 110 char limit.

  9. #9
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $abbrev = substr($row_results['ML']
    If you are getting those results out of a database, take a look at the mysql LEFT() function. Use that to only get 110 chars out of the row, if your full story has 10,000 characters this use a quite a bit of system resource.

  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    There are quite a few code snippets on the php substr page. I am sure that one of them will suit your requirements.
    I agree with foofoonet though, if you have a really large story/article, try to use the SQL syntax to save resources.

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •