慕妹3146593
将数字更改3为以下数字20可获得前20个字,或将其作为参数传递。下面演示了如何获取前3个字:(因此将更3改为20可以更改默认值):function first3words($s, $limit=3) { return preg_replace('/((\w+\W*){'.($limit-1).'}(\w+))(.*)/', '${1}', $s); }var_dump(first3words("hello yes, world wah ha ha")); # => "hello yes, world"var_dump(first3words("hello yes,world wah ha ha")); # => "hello yes,world"var_dump(first3words("hello yes world wah ha ha")); # => "hello yes world"var_dump(first3words("hello yes world")); # => "hello yes world"var_dump(first3words("hello yes world.")); # => "hello yes world"var_dump(first3words("hello yes")); # => "hello yes"var_dump(first3words("hello")); # => "hello"var_dump(first3words("a")); # => "a"var_dump(first3words("")); # => ""