annuaire recherche
欢迎 目录 快速参考图 基本信息 服务器要求 许可协议 变更记录 关于CodeIgniter 安装 下载 CodeIgniter 安装指导 从老版本升级 疑难解答 介绍 开始 CodeIgniter 是什么? CodeIgniter 速记表 支持特性 应用程序流程图 模型-视图-控制器 架构目标 教程 内容提要 加载静态内容 创建新闻条目 读取新闻条目 结束语 常规主题 CodeIgniter URL 控制器 保留字 视图 模型 辅助函数 使用 CodeIgniter 类库 创建你自己的类库 使用 CodeIgniter 适配器 创建适配器 创建核心系统类 钩子 - 扩展框架的核心 自动装载资源 公共函数 URI 路由 错误处理 缓存 调试应用程序 以CLI方式运行 管理应用程序 处理多环境 PHP替代语法 安全 开发规范 类库参考 基准测试类 日历类 购物车类 配置类 Email 类 加密类 文件上传类 表单验证详解 FTP 类 图像处理类 输入类 Javascript 类 语言类 装载类 迁移类 输出类 分页类 模板解析器类 安全类 Session 类 HTML 表格类 引用通告类 排版类 单元测试类 URI 类 User-Agent 类 表单验证 XML-RPC 和 XML-RPC 服务器 Zip 编码类 缓存适配器 适配器参考 适配器 数据库类 Active Record 类 数据库缓存类 自定义函数调用 数据库配置 连接你的数据库 数据库快速入门例子代码 字段数据 数据库维护类 查询辅助函数 数据库类 查询 生成查询记录集 表数据 事务 数据库工具类 JavaScript类 辅助函数参考 数组辅助函数 CAPTCHA 辅助函数 Cookie Helper 日期辅助函数 目录辅助函数 下载辅助函数 Email 辅助函数 文件辅助函数 表单辅助函数 HTML辅助函数 Inflector 辅助函数 语言辅助函数 数字辅助函数 路径辅助函数 安全辅助函数 表情辅助函数 字符串辅助函数 文本辅助函数 排版辅助函数 URL 辅助函数 XML 辅助函数
personnages

CodeIgniter 用户指南 版本 2.1.0

编辑文档、查看近期更改请 登录 或 注册  找回密码
查看原文

字符串辅助函数

该字符串辅助函数为你提供对字符串类型的各种函数。

装载字符串辅助函数

采用如下方式装载该辅助函数:

$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 {
    echo alternator('string one', 'string two');
}

你可以任意添加条目的数量,每一次循环后下一个条目将成为返回值。

for ($i = 0; $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"

 

翻译贡献者: Hex, kkorange
最后修改: 2012-02-06 00:00:18
Article précédent: Article suivant: