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.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.