当前位置: 动力学知识库 > 问答 > 编程问答 >

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

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: