「AppController.php」などにstripTagsメソッド定義し、メソッドを利用することによってHTML・PHPタグ削除機能を一元化できる。
1 2 3 4 5 6 7 8 9 10 11 | protected function stripTags($text, $all_flg=true) { if ($all_flg) { // 全タグ削除(<script></script>含む) return strip_tags(preg_replace('/<script.*<\/script>/', '', trim($text))); } else { // 許可タグ以外削除(<script></script>含む) $allow_tags = '<h1><h2><strong><p><span><br><div>'; // 許可するタグ return strip_tags(preg_replace('/<script.*<\/script>/', '', trim($text)), $allow_tags); } } |
◯ 呼び出し元
1 2 3 | parent::stripTags($text); // 親コントローラーに定義時 or $this->stripTags($text); |