字符串操作

一、字符串简介

字符串是由零个或多个字符构成的一个集合。

字符类型:

(1)数字类型,如1、2、3等

(2)字母类型,如a、b、c等

(3)特殊字符,如#、$、%、等

(4)不可见字符,如\n、\r(回车)、\t等

二、单引号和双引号区别

双引号中的内容会经过php的语法分析器解析,任何变量在双引号中都会被转换为它的值进行输出

单引号所见即所得。

如:

1 <?php
2 $test = "php";
3 $str = "i like $test!";
4 $str1 = 'i like $test!';
5 echo $str ."<br>"; //输出 i like php
6 echo  $str1 ."<br>";//输出 i like $test
7 ?>

三、字符串连接符  (.)

范例:

1 <?php
2 $name ="hello";
3 $url = "php";
4 echo $name.$url."world"."<br>";
5 ?>

四、字符串操作

4.1 去除字符串首尾空格和特殊字符

trim()用于去除字符串首尾空格和特殊字符,同时返回去掉空格和特殊字符后的字符串。

ltrim()去除字符串左边的空格和特殊字符,返回处理后的字符串。

rtrim()去除字符串右边的空格和特殊字符,返回处理后的字符串。

string ltrim(string str[.string charlist])

string rtrim(string str[.string charlist])

string trim(string str[.string charlist]);  str为要操作的字符串,charlist为可选参数,指定从字符串中要删除的字符。不设置该参数,所有可选字符都将被删除。可选字符如下表:

范例:

<?php
 $str = "\r\n\0(:@_@ hello php world @_@:)    ";
 echo trim($str)."<br>";//(:@_@ hello php world @_@:)
 echo trim($str,"\r\n\0(: :)\0");//@_@ hello php world @_@ 
echo ltrim($str,"
\r\n\0(:");
echo rtrim($str,"@_@:)");
 ?>

4.2 转义、还原字符串数据

 (1)手动转义:将一个字符变得没有意义或没有特殊意义。

范例:

1 <?php
2 echo "hello php \"world\"!";//hello php "world"!
3 ?>

(2)自动转义,还原字符串数据

 addslashes()用来为字符串str加入反斜线“\”。

 stripslashes()用来将使用addslashes()函数转义后的字符串str返回原样。

原型:string addslashes(string str);

  string stripslashes(string str);

范例:

1 <?php
2     $str = "hello php 'world'";
3     $a = addslashes($str);
4     echo $a."<br>";//hello php \'world\'
5     echo stripslashes($a);//hello php 'world'
6 ?>

addcslashes()实现转义字符串中的字符,在指定的字符charlist前加上反斜线‘\’。

stripcslashes()函数用来将应用addcslashes()函数转义过的字符串str还原。

原型:

string addcslashes(string str,string charlist);//str为要操作的字符串,charlist指定在字符串中的那些字符串前加上'\'

string stripcslashes(string str,string charlist);

范例:

1 <?php
2 $a="hello php world";
3 echo $a."<br>";//输出hello php world
4 $b = addcslashes($a,"hello php world");
5 echo $b."<br>";//输出\h\e\l\l\o\ \p\h\p\ \w\o\r\l\d
6 $c = stripcslashes($b);
7 echo $c;//输出hello php wo ld
8 ?>

4.3 获取字符串的长度

 int strlen(string str);获取字符串str的长度,并将长度返回。

1 <?php
2 echo strlen("hello php world");
3 ?>

4.4 截取字符串

string substr(sting str,int start,int length);从字符串str的第start个字符开始截取length个字节的字符串,返回截取到的字符串。

1 <?php
2 $text = "hello php world!";
3 echo substr($text,3,5);
4 ?>

4.5 字符串比较

int strcmp(string str1,string str2);比较字符串str1和str2,如果相等返回0,str1>str2,返回》0;str1<str2,返回<0;会区分字符的大小写;

int strcasecmp(string str1,string str2);用法和strcmp一样,但是会区分字符的大小写;

int strnatcmp(string str1,string str2);用法和strcmp一样,它是按照自然排序法进行字符串的比较区分大小写。

int strnatcasecmp(string str1,string str2);不区分大小写;

自然排序法:比较的是字符串中的数字部分,将字符串中的数字按照大小进行比较。

 int strncmp(string str1,string str2,int len);比较字符串str1和str2中的前len个字符。

 1 <?php
 2 $str1 = "1hello php world";
 3 $str2 = "1hello php world";
 4 $str3 = "2hello php world";
 5 $str4 = "2hel    ";
 6 $str5 = "1HELLO PHP WORLD"
 7 echo strcmp($str1,$str2);
 8 echo strncmp($str3,$str4,4);
 9 echo strcasecmp($str1,$str5);
10 echo strnatcmp($str3,$strr4);
11 ?>

4.6 字符串检索

string strstr(string haystack,string needle);在字符串haystack中寻找子串needle,如果有匹配的子串返回匹配到的子串,否则返回false;

int substr_count(string haystack,string needle);统计子串needle在字符串haystack中出现的次数。

1 <?php
2 $str1 = "hello php world";
3 $str1 = "hello";
4 echo strstr($str1,$str2);
5 echo substr_count($str1,"h");
6 ?>

4.7 替换字符串

mixed str_ireplace(mixed search,mixed replace,mixed subject,int &count);在字符串subject中查找字符串search,用字符串replace来替换,并统计替换次数count;不区分大小写;返回替换后的字符串。

 范例:(替换字符串为红色字体)

<?php
$content = "hello php world!";
$str1 = "hello";
echo str_ireplace($str1,"<font color='#FF0000'>".$str1."</font>",$content);
?>

string substr_replace(string str,string replace,int start,int length);

4.8 格式化字符串

 string number_format(float number,int num_decimal_places,string dec_seperator,string thousands_separator);

4.9 分割字符串

array explode(string separator,string str,int limit);

范例:

1 <?php
2 $str = "php@ world@ hello@ok@!";
3 $str_arr = explode("@",$str);
4 print_r($str_arr);//输出Array ( [0] => php [1] => world [2] => hello [3] => ok [4] => ! )
5 echo $str_arr[0];
echo $str_arr[1];
?>

4.10 合成字符串

string implode(stirng glue,array pieces);glue指明使用哪个字符连接字符串,pieces为要连接的字符串数组;

 1 <?php

2 $str = "php@ world@ hello@ok@!";
3 $str_arr = explode("@",$str);
4 print_r($str_arr);//输出Array ( [0] => php [1] => world [2] => hello [3] => ok [4] => ! )
  echo implode("@",$str_arr);//输出php@ world@ hello@ok@!
5 echo $str_arr[0];
echo $str_arr[1]; ?>

 

posted @ 2017-04-13 21:10  高傲的monkey  阅读(271)  评论(0编辑  收藏  举报