PHP中字符串函数整理(一)
来源:互联网 发布:怎样在淘宝上进货 编辑:程序博客网 时间:2024/06/07 07:23
PHP中字符串函数整理(一)
字符串的截取、查找、替换
(1)string chunk_split(string,length,end)
按照length步长分割字符串并在分割的字符串中间添加end```$str = "this is test string!";echo chunk_split($str,2,"=");```
输出结果:th=is= i=s =te=st= s=tr=in=g!=
(2) array explode(separator,string,limit)
字符串打散为数组
$str = "this is test isdda"; print_r(explode('s',$str,0)); print_r(explode('s',$str,2)); print_r(explode('s',$str,-2));
结果:
(3)string implode(separator,array) 数组元素组合为字符串
join()是implode()的别名
2. 字符串的整理(大小写转换,去空格,格式化等)
(1)string addslashes(string)
函数返回在预定义字符之前添加反斜杠的字符串
该函数主要对数据的安全存储,对敏感字符进行转义。
预定义字符是:
单引号(’)
双引号(”)
反斜杠(\)
NULL
(2)string addcslashes(string,characters)
返回在指定字符前添加反斜杠的字符串
该函数对大小写敏感
string–要处理的字符串
characters–指定的添加反斜杠的字符串
(3)stripslashes()
删除由addslashes()函数添加的反斜杠
(4)stripcslashes()
删除由addcslashes()函数添加的反斜杠
(5)删除字符串的空白字符或其他不可见字符
使用语法: string trim(string,charlist)
Charlist有:
- “\0” - NULL
- “\t” - 制表符
- “\n” - 换行
- “\x0B” - 垂直制表符
- “\r” - 回车
- “” - 空格
(6)fprintf(stream,format,arg1,arg2,arg++)
格式化的字符串写入指定的输出流,比fwrite()多了一个格式化数据的作用,返回字写入字符串的长度。其中的f和printf() 具有相同的意义
$number = 9; $str = "Beijing"; $file = fopen("test.txt","w"); echo fprintf($file,"There are %u million bicycles in %s.",$number,$str);
3.字符串的转义和信息函数
(1)string hex2bin(string)
十六进制->ASCII的字符串
(2)string bin2hex(string)
ASCII的字符串->十六进制
(3)string chr(ascii)
从指定的 ASCII 值返回字符
ASCII 值可被指定为十进制值、八进制值或十六进制值。八进制值被定义为带前置 0,而十六进制值被定义为带前置 0x;这个函数的作用就是对应ASCII表转换字符串。
(4)convert_cry_string(string,from,to)
Cyrillic字符集中,不同种类的转换。被支持的 Cyrillic 字符集是:
- k - koi8-r
- w - windows-1251
- i - iso8859-5
- a - x-cp866
- d - x-cp866
- m - x-mac-cyrillic
(5)uuencode编码或解码,需要注意的是uuencoded 数据比原数据大约增大 35%。
(6)mixed count_chars(string,mode)
返回ASCII字符集中字符出现的次数或为出现的次数
(7)crc32(string)
crc32() 函数计算字符串的 32 位 CRC(循环冗余校验)。该函数可用于验证数据完整性。
(8)crypt(string)
crypt() 函数返回使用 DES、Blowfish 或 MD5 算法加密的字符串。
(9)简单了解字符与HTML实体字符之间的转换
举例
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style> table{ border:1px solid #ccc; } td,th{ border:1px solid #ccc; padding:5px 25px; } </style></head><body><?php $str = "t π his'$&<b>jiacu</b>";$str1 = htmlspecialchars($str);$str2 = htmlspecialchars_decode($str1);$str3 = htmlentities($str);$str4 = html_entity_decode($str3);$eof = <<<EOF<table><tr> <th>名称</th> <th>结果</th></tr><tr> <td>原字符串</td> <td>{$str}</td></tr><tr> <td>htmlspecialchars转换</td> <td>{$str1}</td></tr><tr> <td>htmlspecialchars_decode解码</td> <td>{$str2}</td></tr><tr> <td>htmlentities转换</td> <td>{$str3}</td></tr><tr> <td>html_entity_decode解码</td> <td>{$str4}</td></tr></table>EOF;echo $eof;print_r(get_html_translation_table(HTML_ENTITIES));?></body></html>
结果-源代码:
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style> table{ border:1px solid #ccc; } td,th{ border:1px solid #ccc; padding:5px 25px; } </style></head><body><table><tr> <th>名称</th> <th>结果</th></tr><tr> <td>原字符串</td> <td>t π his'$&<b>jiacu</b></td></tr><tr> <td>htmlspecialchars转换</td> <td>t π his'$&<b>jiacu</b></td></tr><tr> <td>htmlspecialchars_decode解码</td> <td>t π his'$&<b>jiacu</b></td></tr><tr> <td>htmlentities转换</td> <td>t π his'$&<b>jiacu</b></td></tr><tr> <td>html_entity_decode解码</td> <td>t π his'$&<b>jiacu</b></td></tr></table>Array( ["] => " [&] => & [<] => < [>] => > [ ] => [¡] => ¡ [¢] => ¢ 等等......)</body></html>
结果-显示:
(10)希伯来字符转换
- PHP中字符串函数整理(一)
- PHP 常用字符串,数组处理函数整理(一)
- PHP函数(一) 字符串函数
- PHP字符串处理函数整理
- [整理]PHP字符串截取函数
- PHP中字符串函数
- php中字符串函数
- php中字符串函数
- 超实用PHP函数总结整理(一)
- php常用函数之字符串函数(一)
- MySQL中concat函数(连接字符串)学习整理
- PHP中MySQL一些函数(一)
- PHP中字符串处理函数
- php 中字符串处理函数
- php中常用的数组函数(整理)
- php常用函数整理系列-字符串处理函数
- [整理]php常用函数及运用场景----字符串函数str
- OpenGL中一些函数的整理(一)
- 读开发规范有感
- mysql 数据库 基本操作(第一章)
- 根据后序数组重建搜索二叉树
- python学习——采用sorted方法对字典的键、值进行排序
- 英文句子单词翻转(StringBuilder,charAt,insert)
- PHP中字符串函数整理(一)
- 本办法学python习题25 更多更多的实践
- 开发遵循手册
- SVN知识梳理和整合
- RocketMQ源码解析-事务消息的二阶段提交
- Bundler 及 PMVS 常见问题&解决方法
- tp3结构应用和模块区分
- 饿汉式单列
- 12个有趣的C语言面试题及答案