字符串輔助函數(shù)
該字符串輔助函數(shù)為你提供對字符串類型的各種函數(shù)。
裝載字符串輔助函數(shù)
采用如下方式裝載該輔助函數(shù):
$this->load->helper('string');
可用函數(shù)如下:
random_string()
根據(jù)你所指定的類型和長度產(chǎn)生一個隨機字符串。可用于生成密碼串或隨機字串。
第一個參數(shù)指定字符串類型,第二個參數(shù)指定其長度。以下為可選字符串類型:
alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1- alpha:? A string with lower and uppercase letters only.
- alnum:? 含有大小寫字母以及數(shù)字。
- numeric:? 數(shù)字字符串。
- nozero:? 不含零的數(shù)字字符串。
- unique:? 用 MD5 and uniqid()加密的字符串。注意:第二個長度參數(shù)在這種類型無效。均返回一個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()
當執(zhí)行一個循環(huán)時,讓兩個或兩個以上的條目輪換使用。范例:
for ($i = 0; $i
{
????echo alternator('string one', 'string two');
}
你可以任意添加條目的數(shù)量,每一次循環(huán)后下一個條目將成為返回值。
for ($i = 0; $i
{
????echo alternator('one', 'two', 'three', 'four', 'five');
}
注意:為了讓多次調用該函數(shù)簡單方便,調用該函數(shù)時請不要帶上實參進行重預置。
repeater()
重復生成你所提交的數(shù)據(jù)。范例:
$string = "\n";
echo repeater($string, 30);
上面的例子將會產(chǎn)生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()
去掉任何出現(xiàn)在字符串開頭或結尾的斜線。范例:$string = "/this/that/theother/";
echo trim_slashes($string); // results in this/that/theother
reduce_multiples()
去掉多余的一個緊接著一個重復出現(xiàn)的特殊字符。范例:
$string="Fred, Bill,, Joe, Jimmy";
$string=reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"
該函數(shù)可以接受如下的形參:
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將會去掉出現(xiàn)在字符串開頭或結尾的字符(即使字符不重復也去掉)。范例:
$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"
?