字符串
注意单引号和双引号的区别
注意转义字符\的使用\\,\",\$
注意使用8进制或16进制字符表示 \xf6
echo "H\xf6me";//需要察看是否支持此类文字编码
---------------------输出结果----------------------------------------
H鰉e
---------------------------------------------------------------------
1.使用printf()和sprintf()创建格式化的输出
printf()直接输出到输出缓冲区
sprintf()的输出作为字符串返回
如printf("输出内容 %.2f\n",$PI());
所有的转换规范都以%开头
数据类型有d-整数,s-字符串,f-浮点数,b-二进制
.2是一个可选的宽度指标,小数点右边输出使用0填充
printf("%.2f",3.14159);
printf("%10.2f",3.14159);
printf("%.10f",3.14159);
printf("%.9s",abcdefghijklmn);
printf("%5.2f,%f,%7.3f\m",3.14159,3.14159,3.14159);
printf("%b %d %f %s \n",123,123,123,"test");
---------------------输出结果----------------------------------------
3.14 3.143.1415900000abcdefghi 3.14,3.141590, 3.142\m1111011 123 123.000000 test
---------------------------------------------------------------------
2.字符串填充
string str_pad(string input原始字串, int length添加后的总长度[, string padding要填充的字符 [, int pad_type]填充类型])
填充类型有添加在左边STR_PAD_LEFT,默认添在右边,填充在两端STR_PAD_BOTH
$index = array("one"=>1,"two"=>155,"three"=>1679);
echo "
";
echo str_pad("这是标题",50," ",STR_PAD_BOTH)."\n";
foreach($index as $inkey=>$inval)
echo str_pad($inkey,30,".").str_pad($inval,20,".",STR_PAD_LEFT)."\n";
echo "
";
---------------------输出结果----------------------------------------
这是标题
one..............................................1
two............................................155
three.........................................1679
---------------------------------------------------------------------
string strtolower(string subject)//转换为小写
string strtoupper(string subject)//转换为大写
string ucfirst(string subject)//首字母大写
string ucwords(string subject)//每个单词首字母大写
string ltrim(string subject)//去左空白
string rtrim(string subject)//去右空白
string trim(string subject)去左右空白,空白包括null,制表符,换行符,回车符和空格
string n12br(string source)//将\n表示的换行符转换为
标记
3.字符串比较
integer strcmp(sting str1,string str2) //str1大于str2返回-1 str1小于str2返回1 str1和str2相等返回0
integer strmcmp(sting str1,string str2,integer length) //第三个参数限制length个字符的比较
print strcmp("aardvark","aardwolf");
print strncmp("aardvark","aardwolf",4);
---------------------输出结果----------------------------------------
-10
---------------------------------------------------------------------
strcasecmp()和strncasecmp()是不区分大小写的比较函数
4.查找和抽取子字符串
string substr(sting source,integer start[,integer length])//从start开始取length个字符
start和length可以使用负值
$var = "abcdefgh";
print substr($var,2);//从0开始计数
print substr($var,2,3);
print substr($var,-1);//从字符串的末尾开始
print substr($var,-5,2);
print substr($var,-5,-2);
---------------------输出结果----------------------------------------
cdefgh
cde
h
de
def
---------------------------------------------------------------------