Skip to main content
June 27, 2009
Question

Stop at a "."

  • June 27, 2009
  • 3 replies
  • 888 views

Is there a function that would take the first one or two sentences out of a string? For example, I have a blog and only want the first two sentence of the latest entries to display on my homepage. I have seen things like this but I wonder if its a function or just entered that way?

Thanks!

    This topic has been closed for replies.

    3 replies

    Inspiring
    June 30, 2009

    Ordinarily, I use regular-expressions for this sort of thing ... but then again, I'm also a PerlMonk.

    Grab some arbitrary number of characters off the left-hand side of the string, then look for the rightmost occurrence of a period, or failing that, the rightmost space.  If either are found (and some wise-ass out there will surely ensure that there is not, just to see what your code does...) truncate the string at that point and add some elipses "...".

    Inspiring
    June 27, 2009

    Something basic like this should do the trick (though it could certainly be refined!). It accounts for periods, exclamations, and question marks as sentence completion markers, which cfSearching caught.

    <cfscript>

         function getFirstTwoSentences( user_text ){

              var first_sentence_end = 0;

              var second_sentence_end = 0;

              var text_length = Len( user_text );

              var first_two_sentences = "";

              first_sentence_end = ReFind( "[\.|\?|!]", Trim( user_text ) );

              second_sentence_end = ReFind( "[\.|\?|!]", Trim( user_text ), first_sentence_end + 1 );

              first_two_sentences = RemoveChars( Trim( user_text ), second_sentence_end + 1, text_length );

              return first_two_sentences;

         }

         paragraph = "This is a paragraph that contains at least three sentences, no? ";

            paragraph = paragraph & "That was the first sentence of the paragraph. ";

         paragraph = paragraph & "The second wasn't so good. Throwing in one more sentence.";

         two_sentences = getFirstTwoSentences( paragraph );

    </cfscript>

    <cfoutput>

         First Two Sentences:<br />

         #two_sentences#<br />

         Full Paragraph:<br />

         #paragraph#

    </cfoutput>

    Inspiring
    June 27, 2009

    I do not believe there is a function like this.  If you are simply looking for periods,  treat the string as a list, delimited by "."  Then use list functions like ListGetAt(...) to grab the first two elements.   Another option is to use functions like this one at cflib.org to grab the first x words of the entry.

    FullLeft

    http://www.cflib.org/index.cfm?event=page.udfbyid&udfid=329

    Inspiring
    June 27, 2009

    Stop at "."


    Of course if the author writes like Immanuel Kant, you might get more text than you bargained for.  Also consider that the first "sentence" may end with other punction marks:  ?, ! , etcetera.  So you may be better off with the FullLeft(...) approach.

    June 27, 2009

    These are good points. Thanks to you both