user198989
user198989

Reputation: 4665

Remove word(s) if they're longer than 30 characters?

I need to remove words from string, if they're longer than 20 characters. I've tried this but it only adds line breaks.

wordwrap($line,30,"",true);

Upvotes: 0

Views: 953

Answers (1)

zerkms
zerkms

Reputation: 255005

$str = 'asd qqqqqqqqqqqqqqqqq weq';

var_dump(preg_replace('~\b\S{5,}\b~', '', $str));

The code above removes everything longer than 5 consecutive non-space characters. Replace 5 with 30 and you'll get what you want

Upvotes: 4

Related Questions