First x words only?
Posted: Wed Sep 17, 2003 2:03 pm
If I've got a variable length string being returned from a database, how can I quickly (no regex if possible!) reduce that string to only the first x words?
In pseudo code:
$TextString = whateverthisfunctionmightbe($TextString,8);
would return "There is a tide in the affairs of" for the given TextString "There is a tide in the affairs of men, which, taken at the flood..."
What I want is exactly like substr, but for words instead of characters. Actually, the issue here is that I am dealing with a small area in which to display between 10 and 150 character results, and don't want to chop off words in the middle. What would be great is if this function could accomodate extreme differences in word length... sort of a combination of substr and the function described above.
In pseudo code:
$TextString = whateverthisfunctionmightbe($TextString,8);
would return "There is a tide in the affairs of" for the given TextString "There is a tide in the affairs of men, which, taken at the flood..."
What I want is exactly like substr, but for words instead of characters. Actually, the issue here is that I am dealing with a small area in which to display between 10 and 150 character results, and don't want to chop off words in the middle. What would be great is if this function could accomodate extreme differences in word length... sort of a combination of substr and the function described above.