Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)归海一刀published in(发表于) 2014/2/17 7:41:58 入门知识:动态网页PHP编程中字符串5个技巧_[PHP教程]
字符串注意单引号和双引号的区别注意转义字符\的使用\\,\",\注意使用8进制或16进制字符表示 \xf6echo "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_BOTHindex = 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..............................................1two............................................155three.........................................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);---------------------输出结果----------------------------------------cdefghcdehdedef
---------------------------------------------------------------------integer strpos(string haystack,string needle[,integer offset])//查找子字符串的位置,返回第一次出现.integer strrpos(string haystack,string needle)//只搜索单个字符(多个字符只取第一个),返回最后一次出现的索引.还有常见的从 字符串中抽取找到的部分 的函数string strstr(string haystack,string needle)//不区分大小写string stristr(string haystack,string needle)//区分大小写string strrchr(string haystack,sting needle)*********** array explode(string separator,string subject[,integer limit])//返回一个字符串数组array implode(string glue,array pieces)//返回一个字符串///////////////////////////代码段////////////////////////////////////////guest = "this is a string";guestArray = explode(" ",guest);var_dump(guestArray);sort(guestArray);echo implode(",",guestArray);////////////////////////////////////////////////////////////////////////---------------------输出结果----------------------------------------array(4) { [0]=> string(4) "this" [1]=> string(2) "is" [2]=> string(1) "a" [3]=> string(6) "string" } a,is,string,this---------------------------------------------------------------------
5.替换字符和子字符串
string substr_replace(string source,string replace,int start[,int length])
赞