出力の文字列の長さを制限するためにこの関数を作成しました。
/* limit the lenght of the string */
function limit_length($content, $limit)
{
# strip all the html tags in the content
$output = strip_tags($content);
# count the length of the content
$length = strlen($output);
# check if the length of the content is more than the limit
if ($length > $limit)
{
# limit the length of the content in the output
$output = substr($output,0,$limit);
$last_space = strrpos($output, ' ');
# add dots at the end of the output
$output = substr($output, 0, $last_space).'...';
}
# return the result
return $output;
}
正常に動作しますが、完璧ではないと思います...たとえば、文字列に次のテキストがあります。
Gender Equality; Radicalisation; Good Governance, Democracy and Human Rights;
これが私が関数を使用する方法です。
echo limit_length($item['pg_description'], 20);
その後、戻ります。
Gender Equality;...
;...
コンテンツ/行内にさらにテキストがあることを人々に伝えたい場合は、見栄えがよくありません。
正規表現を使用して、前に句読点が存在するかどうかを確認してから...
削除することができるかどうかを考えていました。
出来ますか?ある種の「防弾」になるように、関数を改善する式をどのように書くことができますか?
ありがとう。