SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating complete sentences

    Have a sentence:

    This is a test. This is another test.

    I want to grab the first 20 chars. So I do:

    substr($string,0,20)

    Which gives me:

    This is a test. This

    I want to drop anything after the period leaving only the complete sentence. Any idea?

    Thanks
    Silly

  2. #2
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have any ideas on taking whole sentences, except that you should look foor . (dot) characters and split the lines up there.

    PHP Code:
    $line 'This is a sentence. This is another sentence';

    $dotPos strpos $line'.');

    echo 
    substr $line0, ( $dotPos 1) ) . '<BR>';
    echo 
    substr $line, ( $dotPos 2) ) . '<BR>'
    What I am suggesting is you should use strpos to find the location of the dots and then divide the string there. Perhaps explode would be even better?

    Note: you should implement the above in a loop so it loops over multiple senteces (or store all the locations of dots in an array or something)...

    Good luck anyway

    Quaint Tech
    - Blog on web development and web technology.

  3. #3
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out. Sort of. Need logic for sentences ended in ?. Dont have to worry about anything else

    PHP Code:
    $test substr($string,0,200);
    $test strrev($test);
    $test strstr($test,'.');
    $test strrev($test); 
    Your way could be better. Dunno, will relook at it when I get the chance. Thanks!

    Silly

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    San Diego, CA (USA)
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    couldnt you do this:

    PHP Code:
    $cropped substr($string,0,20);
    $parts explode('. '$cropped);
    $result $parts[0]; 
    EDIT: woops, looks like you already solved the problem


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
  •