select first fifteen words

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tolkienarda
    Contributor
    • Dec 2006
    • 316

    select first fifteen words

    hi all

    i need to select the first fifteen words of an article stored in a mysql database. so i have the basic idea laid out but the exact syntax seems a bit confusing. i have a book here on regular expressions and i think it has confused more than before.

    so here is basicaly what i want to do

    select the first word what ever it may be, so /S+ i think should match 1 or more non white space charters and then a /s then repete that fifteen times like so

    /S+/s/S+/s/S+/s/S+/s/S+/s/S+/s/S+/s/S+/s/S+/s/S+/s/S+/s/S+/s/S+/s/S+/s/S+/s

    now i know that there has to be a better way to do that so if anyone knows please relive my ignorance

    eric
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    Do you need to select fifteen words using SQL or are you doing it with PHP?

    Comment

    • tolkienarda
      Contributor
      • Dec 2006
      • 316

      #3
      i am currently trying to do it in php but if there is a good way to do it with sql i would be open to doing it that way.

      eric

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Originally posted by tolkienarda
        i am currently trying to do it in php but if there is a good way to do it with sql i would be open to doing it that way.

        eric
        In PHP just explode() on spaces, and do a for loop to concatenate the first 15 words.

        Comment

        • tolkienarda
          Contributor
          • Dec 2006
          • 316

          #5
          wow that makes life easy

          i thought i was going to have to use regular expressions. and i have been avoiding those and hope i can continue to do so for the remainder of time

          eric

          Comment

          • steven
            New Member
            • Sep 2006
            • 143

            #6
            Uhhh, that's a longwinded way of doing it though.

            It would be much simpler to use a regular expression.

            Code:
            $expression = "/(\w+\s+){15}/";
            $words = "this is a sentence containing a good deal more than fifteen different words but only the first 15 words should be matched!";
            preg_match($expression, $words, $matches);
            The output of $matches[0]

            this is a sentence containing a good deal more than fifteen different words but only

            Comment

            Working...