SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: finding spaces

  1. #1
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    finding spaces

    Hey.
    Im making a news prgram where the first page displays the first 250 chars or so. Is there a way to find the nearest space after 250 chars. Otherwise, if I cut it off at 250 exactly, I might cut a word in half??
    Anyone know?

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $len = strlen($text);
      if ($len > 250) {
        $ext = "...";
        $arr = explode(" ", substr($text, 250, $len));
      }
      print $text . " " . $arr[1] . $ext;
    Let me know if that works okay.

    Edited -- found a bug! Fixed now.
    Last edited by TWTCommish; May 29, 2001 at 07:50.

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Same result slightly different approach:

    PHP Code:
    <?
    $str 
    "sfdgkhsdkf ksdfjh ksfhg ksdfhkhwiwghsdfbg safgsgshfaggrfgbfhgreng jhdd 
            jhsf gsdvf hscbfvsuydgf wdbfjsdy sdf vsdg vsfdjbv sdgb sdjbfsdg sdbf scgb 
            sdhbsjgwrbw87tgfw hbsdfhg sdbfv sdg sdf jhdsb sdgh sdjkhgsd jkgh dskjhgskdjyh 
            fsjkd kjzgh wrkjsfghsidugh wrskfjdhgwsdfuihg wsdfjkhxckjh sfjkh sckjb fv"
    ;

    //If the string length is greater than 250 chars grab
    //everything up to the first space after the 250th character, 
    //or else grab the whole string. If the 250th character is 
    //a space just grab the the string up to the 250th character.
    $newstr = (strlen($str) > 250) ? substr($str0strpos($str' '250))."..." $str."...";
    print 
    $newstr;
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're the man, as usual. Use Freddy's code, it's better.

  5. #5
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    K.. thanx freddy... It works.. but could you explain what its doing? I understand that its grabbing the 250 char or the nearest space.. but how is it done

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well first let me explain the ternary operator

    PHP Code:
    $var 1;
    print (
    $var == 1) ? "The var = 1" "The var doesn't = 1";
    //prints "The var = 1" because $var is indeed set to one

    $var 0;
    print (
    $var == 1) ? "The var = 1" "The var doesn't = 1";
    //prints "The var doesn't = 1" because $var is set to zero 
    So basically if the expression in parenthesis is true than do whatever is right after the ? if its not do whatever is after the :


    So in your code I used this same theory.

    I said if the strlen() of the original string is more than 250 characters then grab every thing starting at 0
    Code:
    substr($str, 0
    up to the first space after the 250th character
    Code:
    , strpos($str, ' ', 250))
    The two function that make this work are substr() and strpos() and each one works like this


    substr(str, offset, end)
    Grab everything from offset to end from str

    So I used strpos for the end arg and that works like this

    strpos(haystack, needle, [offset]);

    It returns th position of the first occurence of needle in haystack so by using this for the end arg of the substr() function I can grab all characters starting with 0 and ending with the first blank space after the 250th character.

    Finally if the strlen of the original string is less than 250 then just return the whole string.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •