Reputation: 75
Q: How to remove words (count limited to 3 chars) of a string in PHP
Example String: "Remove all words of 3 letters" Expected String: "Remove words letters"
Basically, remove all words that are less than equal to 3 counts.
Thanks.
Upvotes: 0
Views: 611
Reputation: 14245
Use the word boundry:
preg_replace('/\b(?:^|\s\w{1,3})\b/', '', $sentance);
Jack's answer only matched all and of.
This expression looks for the word boundaries and will match on that.
Upvotes: 1
Reputation: 173662
Regular expression to select words between 1 and 3 in length:
preg_replace('/\b\w{1,3}\b\s*/', '', $sentence);
The \b
is to match a word boundary, the \w
denotes a "word" character. The \s*
at the end makes sure that excess spaces are removed too.
Upvotes: 3