W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
字符串輔助函數文件包含了一些幫助你處理字符串的函數。
該輔助函數通過下面的代碼加載:
$this->load->helper('string');
該輔助函數有下列可用函數:
random_string([$type = 'alnum'[, $len = 8]])
參數:
返回: A random string
返回類型: string
根據你所指定的類型和長度產生一個隨機字符串。可用于生成密碼或隨機字符串。
第一個參數指定字符串類型,第二個參數指定其長度。有下列幾種字符串類型可供選擇:
使用示例:
echo random_string('alnum', 16);
注解
unique 和 encrypt 類型已經廢棄,它們只是 md5 和 sha1 的別名。
incrementstring($str[, $separator = ''[, $first = 1]])
參數:
返回: An incremented string
返回類型: string
自增字符串是指向字符串尾部添加一個數字,或者對這個數字進行自增。 這在生成文件的拷貝時非常有用,或者向數據庫中某列(例如 title 或 slug)添加重復的內容, 但是這一列設置了唯一索引時。
使用示例:
echo increment_string('file', '_'); // "file_1"
echo increment_string('file', '-', 2); // "file-2"
echo increment_string('file_4'); // "file_5"
alternator($args)
參數:
返回: Alternated string(s)
返回類型: mixed
當執(zhí)行一個循環(huán)時,讓兩個或兩個以上的條目輪流使用。示例:
for ($i = 0; $i < 10; $i++)
{
echo alternator('string one', 'string two');
}
你可以添加任意多個參數,每一次循環(huán)后下一個條目將成為返回值。
for ($i = 0; $i < 10; $i++)
{
echo alternator('one', 'two', 'three', 'four', 'five');
}
注解
如果要多次調用該函數,可以簡單的通過不帶參數重新初始化下。
repeater($data[, $num = 1])
參數:
返回: Repeated string
返回類型: string
重復生成你的數據。例如:
$string = "\n";
echo repeater($string, 30);
上面的代碼會生成 30 個空行。
注解
該函數已經廢棄,使用原生的 str_repeat() 函數替代。
reduce_double_slashes($str)
參數:
返回: A string with normalized slashes
返回類型: string
將字符串中的雙斜線('//')轉換為單斜線('/'),但不轉換 URL 協(xié)議中的雙斜線(例如:http://)
示例:
$string = "http://example.com//index.php";
echo reduce_double_slashes($string); // results in "http://example.com/index.php"
strip_slashes($data)
參數:
返回: String(s) with stripped slashes
返回類型: mixed
移除一個字符串數組中的所有斜線。
示例:
$str = array(
'question' => 'Is your name O\'reilly?',
'answer' => 'No, my name is O\'connor.'
);
$str = strip_slashes($str);
上面的代碼將返回下面的數組:
array(
'question' => "Is your name O'reilly?",
'answer' => "No, my name is O'connor."
);
注解
由于歷史原因,該函數也接受一個字符串參數,這時該函數就相當于 stripslashes() 的別名。
trim_slashes($str)
參數:
返回: Slash-trimmed string
返回類型: string
移除字符串開頭和結尾的所有斜線。例如:
$string = "/this/that/theother/";
echo trim_slashes($string); // results in this/that/theother
注解
該函數已廢棄,使用原生的 trim() 函數代替: | | trim($str, '/');
reduce_multiples($str[, $character = ''[, $trim = FALSE]])
參數:
返回: Reduced string
返回類型: string
移除字符串中重復出現的某個指定字符。例如:
$string = "Fred, Bill,, Joe, Jimmy";
$string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"
如果設置第三個參數為 TRUE ,該函數將移除出現在字符串首尾的指定字符。例如:
$string = ",Fred, Bill,, Joe, Jimmy,";
$string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"
quotes_to_entities($str)
參數:
返回: String with quotes converted to HTML entities
返回類型: string
將字符串中的單引號和雙引號轉換為相應的 HTML 實體。例如:
$string = "Joe's \"dinner\"";
$string = quotes_to_entities($string); //results in "Joe's "dinner""
strip_quotes($str)
參數:
返回: String with quotes stripped
返回類型: string
移除字符串中出現的單引號和雙引號。例如:
$string = "Joe's \"dinner\"";
$string = strip_quotes($string); //results in "Joes dinner"
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: