字符串輔助函數(shù)
該字符串輔助函數(shù)為你提供對(duì)字符串類型的各種函數(shù)。
裝載字符串輔助函數(shù)
采用如下方式裝載該輔助函數(shù):
$this->load->helper('string');
可用函數(shù)如下:
random_string()
根據(jù)你所指定的類型和長度產(chǎn)生一個(gè)隨機(jī)字符串。可用于生成密碼串或隨機(jī)字串。
第一個(gè)參數(shù)指定字符串類型,第二個(gè)參數(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()加密的字符串。注意:第二個(gè)長度參數(shù)在這種類型無效。均返回一個(gè)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()
當(dāng)執(zhí)行一個(gè)循環(huán)時(shí),讓兩個(gè)或兩個(gè)以上的條目輪換使用。范例:
for ($i = 0; $i
{
????echo alternator('string one', 'string two');
}
你可以任意添加條目的數(shù)量,每一次循環(huán)后下一個(gè)條目將成為返回值。
for ($i = 0; $i
{
????echo alternator('one', 'two', 'three', 'four', 'five');
}
注意:為了讓多次調(diào)用該函數(shù)簡單方便,調(diào)用該函數(shù)時(shí)請(qǐng)不要帶上實(shí)參進(jìn)行重預(yù)置。
repeater()
重復(fù)生成你所提交的數(shù)據(jù)。范例:
$string = "\n";
echo repeater($string, 30);
上面的例子將會(huì)產(chǎn)生30個(gè)空行。
reduce_double_slashes()
將字符串中的雙斜線(//)轉(zhuǎn)換為單斜線(/),但不轉(zhuǎn)換形如(http://)的雙斜線。范例:
$string = "http://example.com//index.php";
echo reduce_double_slashes($string); // results in "http://example.com/index.php"
trim_slashes()
去掉任何出現(xiàn)在字符串開頭或結(jié)尾的斜線。范例:$string = "/this/that/theother/";
echo trim_slashes($string); // results in this/that/theother
reduce_multiples()
去掉多余的一個(gè)緊接著一個(gè)重復(fù)出現(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)
第一個(gè)形參用于傳送你所要去掉重復(fù)的字符串。第二個(gè)形參用于傳送你所要去掉的字符。第三個(gè)形參默認(rèn)為 False。如果為True將會(huì)去掉出現(xiàn)在字符串開頭或結(jié)尾的字符(即使字符不重復(fù)也去掉)。范例:
$string=",Fred, Bill,, Joe, Jimmy,";
$string=reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"
quotes_to_entities()
將字符串中的單引號(hào)和雙引號(hào)轉(zhuǎn)換為相應(yīng)的 HTML 字符表示。范例:
$string="Joe's \\\\"dinner\"";
$string=quotes_to_entities($string); //results in "Joe's "dinner""
strip_quotes()
去掉字符串中的單引號(hào)和雙引號(hào)。范例:
$string="Joe's \\\\"dinner\"";
$string=strip_quotes($string); //results in "Joes dinner"
?