2024 年 PHP 日本研討會

字串過濾器

這些過濾器各自的功能都與其名稱相符,並且對應於內建 PHP 字串處理函式的行為。有關特定過濾器的更多資訊,請參閱對應函式的說明頁面。

string.rot13

使用此過濾器等同於透過 str_rot13() 函式處理所有串流資料。

範例 #1 string.rot13

<?php
$fp
= fopen('php://output', 'w');
stream_filter_append($fp, 'string.rot13');
fwrite($fp, "This is a test.\n");
/* 輸出:Guvf vf n grfg. */
?>

string.toupper

使用這個篩選器等同於透過 strtoupper() 函式處理所有串流資料。

範例 #2 string.toupper

<?php
$fp
= fopen('php://output', 'w');
stream_filter_append($fp, 'string.toupper');
fwrite($fp, "This is a test.\n");
/* 輸出:THIS IS A TEST. */
?>

string.tolower

使用這個篩選器等同於透過 strtolower() 函式處理所有串流資料。

範例 #3 string.tolower

<?php
$fp
= fopen('php://output', 'w');
stream_filter_append($fp, 'string.tolower');
fwrite($fp, "This is a test.\n");
/* 輸出:this is a test. */
?>

string.strip_tags

使用這個篩選器等同於透過 strip_tags() 函式處理所有串流資料。它接受兩種形式的參數:一個是包含標籤列表的字串,類似於 strip_tags() 函式的第二個參數;另一個則是一個標籤名稱的陣列。

警告

此功能自 PHP 7.3.0 起已被 _棄用_。強烈建議不要依賴此功能。

範例 #4 string.strip_tags

<?php
$fp
= fopen('php://output', 'w');
stream_filter_append($fp, 'string.strip_tags', STREAM_FILTER_WRITE, "<b><i><u>");
fwrite($fp, "<b>bolded text</b> enlarged to a <h1>level 1 heading</h1>\n");
fclose($fp);
/* 輸出:bolded text enlarged to a level 1 heading */

$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'string.strip_tags', STREAM_FILTER_WRITE, array('b','i','u'));
fwrite($fp, "<b>bolded text</b> enlarged to a <h1>level 1 heading</h1>\n");
fclose($fp);
/* 輸出:bolded text enlarged to a level 1 heading */
?>
新增筆記

使用者貢獻的筆記

此頁面沒有使用者貢獻的筆記。
To Top