「PHP入門」PHPで文字列から限られた単語を表示する機能

phpコード:
function words_limit( $str, $num, $append_str=" ){
$words = preg_split( '/[\s]+/’, $str, -1, PREG_SPLIT_OFFSET_CAPTURE );
if( isset($words[$num][1]) ){
$str = substr( $str, 0, $words[$num][1] ).$append_str;
}
unset( $words, $num );
return trim( $str );>
}

echo words_limit($yourString, 50,’…’);

or

echo words_limit($yourString, 50);

Development

Posted by arkgame