# 字符串輔助函數
字符串輔助函數文件包含了一些幫助你處理字符串的函數。
[TOC=2,3]
## [加載輔助函數](http://codeigniter.org.cn/user_guide/helpers/string_helper.html#id4)
該輔助函數通過下面的代碼加載:
~~~
$this->load->helper('string');
~~~
## [可用函數](http://codeigniter.org.cn/user_guide/helpers/string_helper.html#id5)
該輔助函數有下列可用函數:
random_string([$type = 'alnum'[,?$len = 8]])
參數:
* **$type**?(string) -- Randomization type
* **$len**?(int) -- Output string length
返回: A random string
返回類型: string
根據你所指定的類型和長度產生一個隨機字符串。可用于生成密碼或隨機字符串。
第一個參數指定字符串類型,第二個參數指定其長度。有下列幾種字符串類型可供選擇:
* **alpha**: 只含有大小寫字母的字符串
* **alnum**: 含有大小寫字母以及數字的字符串
* **basic**: 根據?mt_rand()?函數生成的一個隨機數字
* **numeric**: 數字字符串
* **nozero**: 數字字符串(不含零)
* **md5**: 根據?md5()?生成的一個加密的隨機數字(長度固定為 32)
* **sha1**: 根據?sha1()?生成的一個加密的隨機數字(長度固定為 40)
使用示例:
~~~
echo random_string('alnum', 16);
~~~
注解
unique?和?encrypt?類型已經廢棄,它們只是?md5?和?sha1?的別名。
increment_string($str[,?$separator = '_'[,?$first = 1]])
參數:
* **$str**?(string) -- Input string
* **$separator**?(string) -- Separator to append a duplicate number with
* **$first**?(int) -- Starting number
返回: 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)
參數:
* **$args**?(mixed) -- A variable number of arguments
返回: Alternated string(s)
返回類型: mixed
當執行一個循環時,讓兩個或兩個以上的條目輪流使用。示例:
~~~
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($data[,?$num = 1])
參數:
* **$data**?(string) -- Input
* **$num**?(int) -- Number of times to repeat
返回: Repeated string
返回類型: string
重復生成你的數據。例如:
~~~
$string = "\n";
echo repeater($string, 30);
~~~
上面的代碼會生成 30 個空行。
注解
該函數已經廢棄,使用原生的?str_repeat()?函數替代。
reduce_double_slashes($str)
參數:
* **$str**?(string) -- Input string
返回: A string with normalized slashes
返回類型: string
將字符串中的雙斜線('//')轉換為單斜線('/'),但不轉換 URL 協議中的雙斜線(例如:[http://](http://codeigniter.org.cn/user_guide/helpers/string_helper.html))
示例:
~~~
$string = "http://example.com//index.php";
echo reduce_double_slashes($string); // results in "http://example.com/index.php"
~~~
strip_slashes($data)
參數:
* **$data**?(mixed) -- Input string or an array of strings
返回: 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)
參數:
* **$str**?(string) -- Input string
返回: 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]])
參數:
* **$str**?(string) -- Text to search in
* **$character**?(string) -- Character to reduce
* **$trim**?(bool) -- Whether to also trim the specified character
返回: 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)
參數:
* **$str**?(string) -- Input string
返回: 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)
參數:
* **$str**?(string) -- Input string
返回: String with quotes stripped
返回類型: string
移除字符串中出現的單引號和雙引號。例如:
~~~
$string = "Joe's \"dinner\"";
$string = strip_quotes($string); //results in "Joes dinner"
~~~
- 歡迎使用 CodeIgniter
- 安裝說明
- 下載 CodeIgniter
- 安裝說明
- 從老版本升級
- 疑難解答
- CodeIgniter 概覽
- CodeIgniter 將從這里開始
- CodeIgniter 是什么?
- 支持特性
- 應用程序流程圖
- 模型-視圖-控制器
- 設計與架構目標
- 教程 - 內容提要
- 加載靜態內容
- 讀取新聞條目
- 創建新聞條目
- 結束語
- 常規主題
- CodeIgniter URL
- 控制器
- 保留名稱
- 視圖
- 模型
- 輔助函數
- 使用 CodeIgniter 類庫
- 創建類庫
- 使用 CodeIgniter 驅動器
- 創建驅動器
- 創建核心系統類
- 創建附屬類
- 鉤子 - 擴展框架核心
- 自動加載資源
- 公共函數
- 兼容性函數
- URI 路由
- 錯誤處理
- 網頁緩存
- 程序分析
- 以 CLI 方式運行
- 管理你的應用程序
- 處理多環境
- 在視圖文件中使用 PHP 替代語法
- 安全
- PHP 開發規范
- 類庫參考
- 基準測試類
- 緩存驅動器
- 日歷類
- 購物車類
- 配置類
- Email 類
- 加密類
- 加密類(新版)
- 文件上傳類
- 表單驗證類
- FTP 類
- 圖像處理類
- 輸入類
- Javascript 類
- 語言類
- 加載器類
- 遷移類
- 輸出類
- 分頁類
- 模板解析類
- 安全類
- Session 類
- HTML 表格類
- 引用通告類
- 排版類
- 單元測試類
- URI 類
- 用戶代理類
- XML-RPC 與 XML-RPC 服務器類
- Zip 編碼類
- 數據庫參考
- 數據庫快速入門: 示例代碼
- 數據庫配置
- 連接你的數據庫
- 查詢
- 生成查詢結果
- 查詢輔助函數
- 查詢構造器類
- 事務
- 數據庫元數據
- 自定義函數調用
- 數據庫緩存類
- 數據庫工廠類
- 數據庫工具類
- 數據庫驅動器參考
- 輔助函數參考
- 數組輔助函數
- 驗證碼輔助函數
- Cookie 輔助函數
- 日期輔助函數
- 目錄輔助函數
- 下載輔助函數
- 郵件輔助函數
- 文件輔助函數
- 表單輔助函數
- HTML 輔助函數
- 語言輔助函數
- Inflector 輔助函數
- 數字輔助函數
- 路徑輔助函數
- 安全輔助函數
- 表情輔助函數
- 字符串輔助函數
- 文本輔助函數
- 排版輔助函數
- URL 輔助函數
- XML 輔助函數
- 向 CodeIgniter 貢獻你的力量