dan*_*nii 11
实际上,使用函数explode中的"limit"参数(第三个参数,可选,检查函数规范),您可以跳过循环并使代码更简单:
$words_array = explode(" ",$sentence,3);
$new_sencence = ( count($words_array)>2 )?
"<strong>".$words_array[0]." ".$words_array[1]."</strong> ".$words_array[2] :
"<strong>".$sentence."</strong>"; //sentence is 2 words or less, just bold it
Run Code Online (Sandbox Code Playgroud)
编辑:用2个字或更少的句子处理句子