您的位置:首页 > 编程语言 > PHP开发

[PHP]字符串操作

2017-05-04 10:05 127 查看
本文只要介绍了常用的字符串操作,其中包括字符串去首尾空格,获得字符串长度,连接、分隔字符串,转义字符串,截取字符串,字符串查找和替换

从字符串定义到高层字符串处理技巧。

字符串是指游零个或多个字符构成的集合。

单引号&双引号

单引号:所见即所得

双引号:经过PHP语法分析器解析过,变量被替换为值

<?php
$s1 = "Jesus is the life";
$s2 = 'Jesus is the life';
echo '$s1 '.$s1.'<br>'; # $s1 Jesus is the life
echo '$s2 '.$s2.'<br>'; # $s2 Jesus is the life

$attr = 'love';
$str1 = "Jesus is the $attr";
$str2 = 'Jesus is the $attr';
echo '$str1 '.$str1.'<br>'; # $str1 Jesus is the love
echo '$str2 '.$str2.'<br>'; # $str2 Jesus is the $attr
?>


连接符

半角句号“.”:连接两个或以上的字符串成为一个字符串

$love = 'love';
echo 'God '.$love.' you'<br>;  # God love you


双引号中包含字符串

$Jesus = 'God';
$attr = 'love';
echo "$Jesus $attr you";


字符串操作

1. 去除首尾空格和特殊字符

trim()
去除字符串首尾空格和特殊字符,返回操作后的字符串

string trim(string str, [,string charlist])


str
:要操作的字符串对象

charlist
:可选,指定从字符串中删除哪些字符;不设置,则所有可选字符都被删除



ltrim()
去除字符串左边的空格和特殊字符

trim()
语法一致

rtrim()
去除字符串右边的空格和特殊字符

trim()
语法一致

2. 转义、还原字符串数据

手动转义

在字符串中,使用反斜杠(
\
)转义

echo "I\'m Christian";


自动转义

addslashes()
:为字符串str加入反斜杠
\


string addslashed(string str)


addcslashes()
:在指定字符安charlist前加上”\”;

string addcslashed(string str, string charlist)


charlist
指定在哪些字符前加上”\”,
\n,\r
等以C语言转换,其他非字符数字且ASCII码低于32或高于126的字符安转换为八进制表示。需要明确在开始和结束范围内的字符串

自动还原字符串数据

stripslashes()
:将
addslashes()
转义后的字符串
str
返回原样

string stripslashes(string str)


stripcslashes()
:将
addcslashes()
转义后的字符串
str
返回原样

string stripcslashes(string str)


<?php
$str = "不从恶人的计谋,不站罪人的道路";
echo $str.'<br>'; # 不从恶人的计谋,不站罪人的道路
$slashesed = addcslashes($str, "不站罪人的道路");
echo $slashesed.'<br>'; #\344\270\215\344���\201�\344\272\272\347\...
$restore = stripcslashes($slashesed);
echo $restore.'<br>'; # 不从恶人的计谋,不站罪人的道路
?>


手动转义适用于简单的字符串

自动转义适用于数据量较大的字符串

3. 获取字符串的长度

int strlen(string str)


汉字占两个字符,数字、英文、小数点、下划线和空格占一个字符

4. 截取字符串

string substr(string str, int start [, int length])


start
的指定位置从0开始计算



截取中文字符串的个数是奇数的话会出现乱码

5. 比较字符串

相等返回0;

str1
>
str2
, 返回值 > 0;

str1
<
str2
, 返回值 < 0;

按照字节

strcmp()
区分字母大小写

int strcmp(string str1, string str2)


strcasecmp()
不区分大小写,其他和
strcmp
一致

按照自然排序法

strnatcmp()
比较字符串中的数字部分,区分字母大小写

int strnatcmp(string str1, string str2)


strnatcasecmp()
不区分大小写,其他和
strnatcmp
一致

指定位置

strncmp()
比较字符串的前n个字符,区分大小写

int strncmp(string str1, string str2, int len)


6. 查找字符串

查找指定关键字

strstr()
正序搜索一个字符串在另一个字符串中的第一次出现

string strstr(string haystack, string needle)


haystack
指定从哪个字符串进行搜索

needle
指定搜索的对象。如果是数值,则搜索与这个数值的ASCII相匹配的字符

找到所搜索的字符串,数返回字符串的其余部分(从匹配点)。

未找到,则返回 false。

strchr
逆序检索子串

substr_count()
检索子串出现的次数

int substr_count(string haystack, string needle)


一般用于搜索引擎

7. 替换字符串

str_ireplace()
使用新的子串替换原始字符串中被指定要替换的字符串

mixed str_ireplace(mixed search, mixed replace, mixed subject[, int &count])




将所有在参数
subject
中出现的参数
search
以参数
replace
取代

不区分大小写

str_replace()
区分大小写,其他和
str_ireplace()
一致

substr_replace()
对指定字符串中的部分字符串中进行替换

string substr_replace(string str, string repl, int start[, int length])




如果
start
是负数,而参数
length
>=
start
,那么
length
== 0

8. 格式化字符串

数字字符串格式化

number_format()


string number_format(float number[, int num_decimal_places,][string dec_seperator, string thousands_separator])


参数可以为1,2,4,但不能为3。

一个参数:舍去小数点后的值,第3位数字就会以逗号隔开

二个参数:格式化到小数点后第
num_decimal_places
位,第3位数字就会以逗号隔开

四个参数:格式化到小数点后第
num_decimal_places
位,
dec_seperator
用来替代小数点,
thousands_separator
取代第3位数字隔开的逗号

$num = 5211314.334;
echo number_format($num).'<br>'; # 5,211,314
echo number_format($num,2).'<br>'; # 5,211,314.33
echo number_format($num,3,'.','.').'<br>'; 5.211.314.334


9. 分隔字符串

explode()
按照指定规则对字符串分割,返回数组

array explode(string sqparator, string str[, int limit])




$str="Jesus is the way@Jesus is the truth@Jesus is the life";
$str_arr=explode("@",$str);
print_r($str_arr); # Array ( [0] => Jesus is the way [1] => Jesus is the truth [2] => Jesus is the life )


10. 合成字符串

implode()
可以将数组的内容组合成新字符串

string implode(string glue, array pieces)


glue
指定分隔符

pieces
指定要被合并的数组

$str="Jesus is the way@Jesus is the truth@Jesus is the life";
echo $str.'<br>';
$str_arr=explode("@",$str);
print_r($str_arr);
echo '<br>';
$array=implode("@",$str_arr);
echo $array; # Jesus is the way@Jesus is the truth@Jesus is the life
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 字符串