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

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) 字符串打散为数组

参数

描述

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));

结果:

809b9856b6c4c6fbffbe86c52ba63b2e.png

(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 实体转换为字符

举例

Documenttitle>

border:1px solid #ccc;

}

td,th{

border:1px solid #ccc;

padding:5px 25px;

}style>

head>

$str = "t π his'$&jiacu";

$str1 = htmlspecialchars($str);

$str2 = htmlspecialchars_decode($str1);

$str3 = htmlentities($str);

$str4 = html_entity_decode($str3);

$eof = <<名称

结果

原字符串

{$str}

htmlspecialchars转换

{$str1}

htmlspecialchars_decode解码

{$str2}

htmlentities转换

{$str3}

html_entity_decode解码

{$str4}

EOF;

echo $eof;

print_r(get_html_translation_table(HTML_ENTITIES));

?>

body>

html>

结果-源代码:

Documenttitle>

border:1px solid #ccc;

}

td,th{

border:1px solid #ccc;

padding:5px 25px;

}style>

head>

名称th>

结果th>

tr>

原字符串td>

t π his'$& jiacub>td>

tr>

htmlspecialchars转换td>

t π his'$& jiacutd>

tr>

htmlspecialchars_decode解码td>

t π his'$& jiacub>td>

tr>

htmlentities转换td>

t π his'$& jiacutd>

tr>

html_entity_decode解码td>

t π his'$& jiacub>td>

tr>

table>

Array

(

["] => "

[&] => &

[<] => <

[>] => >

[ ] =>

[¡] => ¡

[¢] => ¢

等等......

)

body>

html>

结果-显示:

b994d18ab434410a821110d0dd69bbd8.png

(10)希伯来字符转换

函数

功能

hebrev(string,maxcharline)

把希伯来文本从右至左的流转换为左至右的流

hebrevc(string,maxcharline)

把希伯来文本从右至左的流转换为左至右的流。同时,把新行(\n)转换为

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值