大文字又は小文字へ変換(upper, lower)

広告

「upper」は変数に格納されている半角英文字を大文字へ変換します。書式は次の通りです。

{$変数名|upper}

また「lower」は変数に格納されている半角英文字を小文字へ変換します。書式は次の通りです。

{$変数名|lower}

どちらの修飾子も半角英文字以外の値に適用しても何も行われません。

具体的には次のように記述します。

名前は {$name|upper} です。
住所は {$name|lower} です。

サンプルプログラム

では簡単なサンプルプログラムを作成して試してみます。

sample2-1.php

<?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', '修飾子のテスト');
$smarty->assign('name', 'Yamada, Tarou');
$smarty->assign('address', 'Tokyo, Japan');

$smarty->display('sample2-1.tpl');

?>

上記を「sample2-1.php」の名前で「(Apacheドキュメントルート)¥smarty¥modifier」に保存します。

sample2-1.tpl

{* Smarty modifier/sample3-1.tpl *}
<html>
<head>
<title>Smarty Test</title>
</head>
<body>

<h1>{$title}</h1>
<p>名前は {$name} ({$name|upper})です。</p>
<p>住所は {$address} ({$address|lower})です。</p>

</body>
</html>

上記を「sample2-1.tpl」の名前で「D:¥smartysample¥modifier¥templates」に保存します。

そしてブラウザから「http://localhost/smarty/modifier/sample2-1.php」へアクセスして下さい。

大文字又は小文字へ変換(upper, lower)

今回は名前及び住所について変換前と変換後をそれぞれ表示しました。

( Written by Tatsuo Ikura )