amit
amit

Reputation: 10261

substr like function to work on complete words

i am using substr to trim the first 100 characters from the string. however i need a function that can trim a particular number of words, instead of characters from a string?

$trimmed_details = substr($row->details, 0, 200).'...';

is there a built in function to do that?

Upvotes: 0

Views: 179

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 157863

No, there is no builtin function in both PHP and mysql. Because there is no data type "word (of language)" in the either system. You can invent some regexp to do this, but I'd avise to just give up and use old char-based trimming.

Upvotes: 1

Andrew Hare
Andrew Hare

Reputation: 351516

Try this:

join(" ", explode(" ", $yourStr, 100));

Upvotes: 1

Related Questions