HTMLタグを置換(strip_tags)
「strip_tags」は文字列に含まれるHTMLタグを全て1つの空白に置き換えます。書式は次の通りです。
{$変数名|strip_tags[:空白に置き換えるかどうか]}
HTMLタグをエスケープするのではなくタグそのものを置換することに注意して下さい。
HTMLタグとは「<」と「>」で囲まれたものです。デフォルトでは1つの空白に置き換えられますが、パラメータに「false」を指定した場合空白ではなく空文字("")に置き換えられます。
具体的には次のように記述します。
住所は {$address|strip_tags} です
空文字に置き換えたい場合は次のように記述します。
住所は {$address|strip_tags:false} です
サンプルプログラム
では簡単なサンプルプログラムを作成して試してみます。
<?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('htmltext', '<a href="http://www.yahoo.co.jp/">Yahoo</a>'); $smarty->display('sample7-1.tpl'); ?>
上記を「sample7-1.php」の名前で「(Apacheドキュメントルート)¥smarty¥modifier」に保存します。
{* Smarty modifier/sample7-1.tpl *} <html> <head> <title>Smarty Test</title> </head> <body> <h1>{$title}</h1> <p>Yahooへのリンクは{$htmltext}です</p> <p>Yahooへのリンクは{$htmltext|strip_tags}です</p> <p>Yahooへのリンクは{$htmltext|strip_tags:false}です</p> </body> </html>
上記を「sample7-1.tpl」の名前で「D:¥smartysample¥modifier¥templates」に保存します。
そしてブラウザから「http://localhost/smarty/modifier/sample7-1.php」へアクセスして下さい。
表示されたHTMLページのソースを見てみるとHTMLタグが置き換わっている事が分かります。
( Written by Tatsuo Ikura )