PHP字符串
字符串的声明
可以使用双引号,也可以使用单引号。
双引号可以解析变量,可以使用所有的转义字符。
字符串的使用
分割、匹配、查找、替换
系统函数:如果是其他类型的数据,也可以使用字符串处理函数。处理的方式就是先将其他类型自动转成字符串,再处理。
字符串可以像数组一样,通过下标,来访问每个字符,但不是数组。
字符串除了有英文字符,还有中文。utf-8
编码下,一个汉字占三个字节。
/* 猫斯基 maosiji.com */ // 统计数组长度的,有警告 // echo count( "abc" ).'<br>'; // 1 echo strlen( 'abc' ).'<br>'; // 3 // 整型转成字符串,然后再计算长度 echo strlen( 10000 ).'<br>'; // 5 $str = "hello"; // 字符串可以像数组一样,通过下标,来访问每个字符,但不是数组。 echo $str[0].$str[1].'<br>'; // he echo $str{0}.$str{1}.'<br>'; // he // utf-8编码下,一个汉字占三个字节 echo strlen( "中国" ); // 6
/* 猫斯基 maosiji.com */ $str = "hello"; // 通过下标改值 $str{2} = 'm'; // 字符串可以像数组一样,通过下标,来访问每个字符,但不是数组。 echo $str.'<br>'; // hemlo // 只对第一个字符有效,其他的自动舍弃 $str[2] = 'mao'; echo $str.'<br>'; // hemlo $int = 888; $arr = array( 'one'=>666, 'two'=>777 ); echo "1111111 $int 111111".'<br>'; // 1111111 888 111111 echo "1111111{$arr['one']}111111".'<br>'; // 1111111666111111 echo "1111111 $arr[one] 111111".'<br>'; // 1111111 666 111111 class Demo { var $name = 222; } $d = new Demo; echo "1111111{$d->name}111111".'<br>'; // 1111111222111111 echo "1111111 $d->name 111111".'<br>'; // 1111111 222 111111
中文的分割
$str = "hello world"; $str1 = "你好 世界"; echo substr( $str, 0, 7 ).'<br>'; // hello w echo mb_substr( $str1, 0, 2, 'utf-8' ).'<br>'; // 你好
echo 与 print 的区别
/* 猫斯基 wp.maosiji.com */ // 指令方式 echo "11111111<br>"; // 函数方式 echo ("11111111<br>"); // print 有返回值,echo 没有返回值 var_dump( print "11111111<br>" ); echo '<br>'; print "11111111<br>"; print ("11111111<br>"); // echo 可以打印多个值 echo "aa","bb","cc";
exit die
/* 猫斯基 wp.maosiji.com */ exit('aaa'); die('bbb'); // 正常退出 die(0); exit(0); // 意外退出 可以写 1 到 254 的整数 die(1); exit(254);
printf sprintf
%%
返回百分比符号
%b
返回二进制数
%c
返回依照ASCII值的字符
%d
返回带符号的十进制数
%e
返回科学计数法的数
%u
返回无符号十进制数
%f
返回浮点数
%F
返回浮点数
%o
返回八进制数
%s
返回字符串
%x
返回十六进制数
%X
返回十六进制数
/* 猫斯基 wp.maosiji.com */ $int = 100; printf( '%c', $int ); echo '<br>'; printf( 'sfsdfw%ddfdfsd%csfsdgfds', $int, $int ); echo '<br>'; printf( '%%, %d, %c, %b, %e, %u, %f, %.2f', $int, $int, $int, $int, $int, $int, $int, $int ); echo '<br>'; echo chr($int).'<br>'; /* d sfsdfw100dfdfsddsfsdgfds %, 100, d, 1100100, 1.000000e+2, 100, 100.000000, 100.00 d */
/* 猫斯基 wp.maosiji.com */ $int = 100; $str = sprintf( '%%, %d, %c, %b, %e, %u, %f, %.2f', $int, $int, $int, $int, $int, $int, $int, $int ); echo str; /* %, 100, d, 1100100, 1.000000e+2, 100, 100.000000, 100.00 */
trim rtrim ltrim 去除两边字符
/* 猫斯基 wp.maosiji.com */ $str = ' hello world98adw!! '; // 去除右边空格 echo rtrim( $str ).'---'.strlen( rtrim( $str ) ).'<br>'; $str = ' hello world98adw!!'; // 去除最右边的两个叹号 echo rtrim( $str, '!!').'---'.strlen( rtrim( $str, '!!') ).'<br>'; $str = ' hello world98adw!! '; // 去除左边空格 echo ltrim( $str ).'---'.strlen( ltrim( $str ) ).'<br>'; // 去除两边空格 echo trim( $str ).'---'.strlen( trim( $str ) ).'<br>'; // 去除两边0到9的数字、a到z的字母、空格 echo trim( $str, "0..9a..z ").'<br>'; /* hello world98adw!!---19 hello world98adw---17 hello world98adw!! ---19 hello world98adw!!---18 !! */
str_pad 字符串补足
/* 猫斯基 wp.maosiji.com */ $str = 'hello'; // 用空格补足字符串的长度 $strnew = str_pad( $str, 10 ); $strnew2 = str_pad( $str, 10, ' ' ); echo $strnew.' --- '.strlen( $strnew ).'<br>'; echo $strnew2.' --- '.strlen( $strnew2 ).'<br>'; // 用#补足字符串10个长度 $strnew2 = str_pad( $str, 10, '#' ); // 默认在右边补足 $strnew3 = str_pad( $str, 10, '#', STR_PAD_RIGHT ); echo $strnew2.' --- '.strlen( $strnew2 ).'<br>'; echo $strnew3.' --- '.strlen( $strnew3 ).'<br>'; // 用#在左边补足字符串10个长度 $strnew4 = str_pad( $str, 10, '#', STR_PAD_LEFT ); echo $strnew4.' --- '.strlen( $strnew4 ).'<br>'; // 用#在两边补足字符串10个长度 $strnew4 = str_pad( $str, 10, '#', STR_PAD_BOTH ); echo $strnew4.' --- '.strlen( $strnew4 ).'<br>'; /* hello --- 10 hello --- 10 hello##### --- 10 hello##### --- 10 #####hello --- 10 ##hello### --- 10 !! */
大小写转换
/* 猫斯基 wp.maosiji.com */ $str = 'hello World maosiji'; // 转成小写 echo strtolower( $str ).'<br>'; // 转成大写 echo strtoupper( $str ).'<br>'; // 将第一个字符转成大写 echo ucfirst( $str ).'<br>'; // 每个字的第一个字母转成大写 echo ucwords( $str ).'<br>'; /* hello world maosiji HELLO WORLD MAOSIJI Hello World maosiji Hello World Maosiji !! */
提交表单时的字符串
<form action="" method="post"> title:<input type="text" name="title" value="" /> <input type="submit" name="submit" value="提交" /> </form> <?php /* 猫斯基 wp.maosiji.com */ if ( isset($_POST['submit']) ) { $title = $_POST['title']; // 将双引号转义 $title = addslashes( $title ); // 将转义的去掉\转义字符 $title = stripslashes( $title ); // html标签转成实体 echo htmlspecialchars($title).'<br>'; // 删除html标签 echo strip_tags( $title ); // 只删除b和u标签 echo strip_tags( $title, "<b><u>" ); }
nl2br
/* 猫斯基 wp.maosiji.com */ $str = "i i i i i i \n"; $str .= "a a a a a \n"; echo $str.'<br>'; echo nl2br( $str ); /* i i i i i i a a a a a i i i i i i a a a a a */
字符串格式处理
/* 猫斯基 wp.maosiji.com */ $str = "hello world"; // 前后颠倒 echo strrev( $str ).'<br>'; // 字符串长度 echo strlen( $str ).'<br>'; $str2 = "0123456789"; // 格式 echo number_format( $str2 ).'<br>'; echo number_format( $str2, 2 ).'<br>'; echo number_format( $str2, 2, ".", "," ).'<br>'; // md5加密 多层加密存储密码 echo md5( md5( md5( $str ).'maosiji').'com' ).'<br>'; /* dlrow olleh 11 123,456,789 123,456,789.00 123,456,789.00 534765312785f1ce561ddcba5aa48dd7 */
字符串比较处理
/* 猫斯基 wp.maosiji.com */ $str = "hello world"; $str2 = "hello world"; // 等号比较 if ( $str == $str2 ) { echo '相等';} else { echo '不相等';} $str = "hello world maosiji"; $str2 = "hello world MAOSIJI"; // 等号比较 if ( strtoupper($str) == strtoupper($str2) ) { echo '相等';} else { echo '不相等';}
/* 猫斯基 wp.maosiji.com */ $str = "hello world"; $str2 = "hello World"; // 二进制安全比较 // 字符串1小于字符串2,返回负数; // 字符串1大于字符串2,返回正数; // 字符串1等于字符串2,返回0 switch ( strcmp( $str, $str2 ) ) { case 0: echo "{$str} 等于 {$str2}"; break; case -1: echo "{$str} 小于 {$str2}"; break; case 1: echo "{$str} 大于 {$str2}"; break; } // 不区分大小写 switch ( strcasecmp( $str, $str2 ) ) { case 0: echo "{$str} 等于 {$str2}"; break; case -1: echo "{$str} 小于 {$str2}"; break; case 1: echo "{$str} 大于 {$str2}"; break; }
/* 猫斯基 wp.maosiji.com */ $str = "hello11.txt"; $str2 = "hello1.txt"; // 按自然顺序比较 switch ( strnatcmp( $str, $str2 ) ) { case 0: echo "{$str} 等于 {$str2}"; break; case -1: echo "{$str} 小于 {$str2}"; break; case 1: echo "{$str} 大于 {$str2}"; break; } // 不区分大小写 switch ( strnatcasecmp( $str, $str2 ) ) { case 0: echo "{$str} 等于 {$str2}"; break; case -1: echo "{$str} 小于 {$str2}"; break; case 1: echo "{$str} 大于 {$str2}"; break; }