センテンス数をカウント(count_sentences)
広告
「count_sentences」は対象の変数に含まれるセンテンス数をカウントします。書式は次の通りです。
{$変数名|count_sentences}
センテンスの区切りはドット(.)です。その為、英語の文章には利用できますが日本語の文章では通常センテンスの区切りに「。」を使いますのでセンテンス数を数える場合はこの修飾子を使うだけでは難しいと思います。
具体的には次のように記述します。
センテンス数は {$var|count_sentences} 個です
サンプルプログラム
では簡単なサンプルプログラムを作成して試してみます。
<?php require_once('Smarty.class.php'); $smarty = new Smarty(); $smarty->template_dir = 'd:/smartysample/modifier/templates/'; $smarty->compile_dir = 'd:/smartysample/modifier/templates_c/'; $smarty->config_dir = 'd:/smartysample/modifier/configs/'; $smarty->cache_dir = 'd:/smartysample/modifier/cache/'; $smarty->assign('title', '修飾子のテスト'); $msg=<<<EOT Smarty is a template engine for PHP. More specifically, it facilitates a managable way to separate application logic and content from its presentation. This is best described in a situation where the application programmer and the template designer play different roles, or in most cases are not the same person. EOT; $smarty->assign('msg', $msg); $smarty->display('sample17-1.tpl'); ?>
上記を「sample17-1.php」の名前で「(Apacheドキュメントルート)¥smarty¥modifier」に保存します。
{* Smarty modifier/sample17-1.tpl *} <html> <head> <title>Smarty Test</title> </head> <body> <h1>{$title}</h1> <p>{$msg}</p> <p>センテンス数は {$msg|count_sentences} 個です</p> </body> </html>
上記を「sample17-1.tpl」の名前で「D:¥smartysample¥modifier¥templates」に保存します。
そしてブラウザから「http://localhost/smarty/modifier/sample17-1.php」へアクセスして下さい。
( Written by Tatsuo Ikura )