String 輔助函數
String 輔助函數檔包含協助使用字串的函數。
載入輔助函數
使用下列的程式碼載入補助程式:
$this->load->helper('string');
下列為可以使用的函數:
random_string()
根據你所指定字串的類型和長度產生一個隨機字串。可用於產生密碼字串或隨機字串。
第一個參數指定字串類型,第二個參數指定字串長度。以下為可以使用的字串類型:
alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1- alpha: A string with lower and uppercase letters only.
- alnum: 含有大小寫字母以及數字。
- numeric: 數字字串。
- nozero: 不含零的數字字串。
- unique: 用 MD5 and uniqid()加密的字串。注意:第二個長度參數在這種類型無效。均傳回一個固定32位長度的字串。
- sha1: An encrypted random number based on do_hash() from the security helper.
使用範例:
echo random_string('alnum',16);
increment_string()
Increments a string by appending a number to it or increasing the number. Useful for creating "copies" or a file or duplicating database content which has unique titles or slugs.
Usage example:
echo increment_string('file', '_'); // "file_1"
echo increment_string('file', '-', 2); // "file-2"
echo increment_string('file-4'); // "file-5"
alternator()
當執行一個迴圈時,讓兩個或兩個以上的項目交替使用。參考範例:
for ($i = 0; $i < 10; $i++)
{
echo alternator('string one','string two');
}
你可以任意增加項目的數量,每一次迴圈執行後,下一個項目將成為傳回值。
for ($i = 0; $i < 10; $i++)
{
echo alternator('one','two','three','four','five');
}
注意: 為了讓多次呼叫該函數簡單方便,呼叫該函數時,只需呼叫函數,不用重新初始化參數。
repeater()
重複產生你所指定次數的資料。參考範例:
$string = "\n";
echo repeater($string,30);
上面的例子將會產生30列空白列。
reduce_double_slashes()
將字串中的雙斜線(//)轉換為單斜線(/),但不轉換如http://中的雙斜線。參考範例:
$string = "http://example.com//index.php";
echo reduce_double_slashes($string); // results in "http://example.com/index.php"
trim_slashes()
去除任何出現在字串開頭或結尾的斜線。參考範例:
$string = "/this/that/theother/";
echo trim_slashes($string); // results in this/that/theother
reduce_multiples()
去除多餘的重複出現特殊字元。參考範例:
$string="Fred,Bill,,Joe,Jimmy";
$string=reduce_multiples($string,","); //results in "Fred,Bill,Joe,Jimmy"
本函數接受以下參數:
reduce_multiples(string: text to search in,string: character to reduce,boolean: whether to remove the character from the front and end of the string)
第一個參數用於傳送你所要去除重複的字串。第二個參數用於傳送你所要去除的字元。第三個參數預設為 False。如果為True將會去除出現在字串開頭或結尾的字元(即使字元不重複也會去除)。參考範例:
$string=",Fred,Bill,,Joe,Jimmy,";
$string=reduce_multiples($string,",",TRUE); //results in "Fred,Bill,Joe,Jimmy"
quotes_to_entities()
將字串中的單引號和雙引號轉換為對應的 HTML 字元符號表示。範例:
$string="Joe's \"dinner\"";
$string=quotes_to_entities($string); //results in "Joe's "dinner""
strip_quotes()
去除字串中的單引號和雙引號。 參考範例:
$string="Joe's \"dinner\"";
$string=strip_quotes($string); //results in "Joes dinner"