PHP中字符串函数整理(一)

来源:互联网 发布:怎样在淘宝上进货 编辑:程序博客网 时间:2024/06/07 07:23

PHP中字符串函数整理(一)

  1. 字符串的截取、查找、替换
    (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) 字符串打散为数组

参数 描述 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)

函数名 功能 trim() 删除两侧的 ltrim() 删除左边 rtrim() 删除右边 chop() 删除右边


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%。

函数 功能 convert_uuencode(string) 编码 convert_uudecode(string) 解码


(6)mixed count_chars(string,mode) 返回ASCII字符集中字符出现的次数或为出现的次数

参数 描述 string 必需。规定要检查的字符串 mode 可选,规定返回模式。默认是0 0 - 数组,ASCII 值为键名,出现的次数为键值 1 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数大于 0 的值 2 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数等于 0 的值 3 - 字符串,带有所有使用过的不同的字符 4 - 字符串,带有所有未使用过的不同的字符


(7)crc32(string) crc32() 函数计算字符串的 32 位 CRC(循环冗余校验)。该函数可用于验证数据完整性。
(8)crypt(string) crypt() 函数返回使用 DES、Blowfish 或 MD5 算法加密的字符串。
(9)简单了解字符与HTML实体字符之间的转换

函数 功能 htmlspecialchars() 把预定义字符转换成HTML实体 htmlspecialchars_decode() 把一些预定义的 HTML 实体转换为字符 get_html_translation_table() 输出 htmlspecialchars 函数使用的翻译表 htmlentities() 把字符转换为 HTML 实体 html_entity_decode() 把 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'$&amp;&lt;b&gt;jiacu&lt;/b&gt;</td></tr><tr>    <td>htmlspecialchars_decode解码</td>    <td>t π his'$&<b>jiacu</b></td></tr><tr>    <td>htmlentities转换</td>    <td>t &pi; his'$&amp;&lt;b&gt;jiacu&lt;/b&gt;</td></tr><tr>    <td>html_entity_decode解码</td>    <td>t π his'$&<b>jiacu</b></td></tr></table>Array(    ["] => &quot;    [&] => &amp;    [<] => &lt;    [>] => &gt;    [ ] => &nbsp;    [¡] => &iexcl;    [¢] => &cent;    等等......)</body></html>

结果-显示:

这里写图片描述

(10)希伯来字符转换

函数 功能 hebrev(string,maxcharline) 把希伯来文本从右至左的流转换为左至右的流 hebrevc(string,maxcharline) 把希伯来文本从右至左的流转换为左至右的流。同时,把新行(\n)转换为 <br>