PHP学习系列(1)——字符串处理函数(2)

时间:2014-10-10 13:22:04   收藏:0   阅读:279

6、chunk_split() 函数把字符串分割为一连串更小的部分。本函数不改变原始字符串。

语法:chunk_split(string,length,end)

参数:

string——必需。规定要分割的字符串。

length——可选。一个数字,定义字符串块的长度。

end——可选。字符串值,定义在每个字符串块之后放置的内容。

例子 1

本例分隔每个字符,并添加 ".":

<?php
$str = "Hello world!";
echo chunk_split($str,1,".");
?>

输出:

H.e.l.l.o. .w.o.r.l.d.!.
例子 2

本例将在六个字符之后分割一次字符串,并添加 "...":

<?php
$str = "Hello world!";
echo chunk_split($str,6,"...");
?>

输出:

Hello ...world!...
7、convert_cyr_string() 函数把字符由一种 Cyrillic 字符转换成另一种。

被支持的 Cyrillic 字符集是:

语法
convert_cyr_string(string,from,to)
8、convert_uudecode() 函数对 uuencode 编码的字符串进行解码。
语法:convert_uudecode(string)

例子

在本例中,我们将通过使用 convert_uudecode() 对 uuencode 编码的字符串进行解码:

<?php
$str = ",2&5L;&\@=V]R;&0A `";
echo convert_uudecode($str);
?>

输出:

Hello world!
9、convert_uuencode() 函数使用 uuencode 算法对字符串进行编码。
语法:convert_uuencode(string)

注意:

本函数把所有字符串(包括二进制的)转换为可打印的字符串,确保其网络传输的安全。uuencode 的字符串比原字符串增大大约 35%。

例子

在本例中,我们将使用 convert_uuencode() 对字符串进行编码:

<?php
$str = "Hello world!";
echo convert_uuencode($str);
?>

输出:

,2&5L;&\@=V]R;&0A `

10、count_chars() 函数返回字符串所用字符的信息。

语法
count_chars(string,mode)

参数
描述

string
必需。规定要检查的字符串。

mode

可选。规定返回模式。默认是 0。有以下不同的返回模式:

实例

例子 1

在本例中,我们将使用 count_chars() 来检查字符串,返回模式设置为 1:

<?php
$str = "Hello World!";
print_r(count_chars($str,1));
?>

输出:

Array
(
[32] => 1
[33] => 1
[72] => 1
[87] => 1
[100] => 1
[101] => 1
[108] => 3
[111] => 2
[114] => 1
)
例子 2

在本例中,我们将使用 count_chars() 来检查字符串,返回模式设置为 3:

<?php
$str = "Hello World!";
echo count_chars($str,3);
?>

输出:

 !HWdelor
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!