问题描述:

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);

网友答案:
$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

相关阅读:
Top