字符串
注意单引号和双引号的区别
注意转义字符\的使用\\,\",\$
注意使用8进制或16进制字符表示 \xf6
echo "H\xf6me";//需要察看是否支持此类文字编码<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> echo </font><font color="#DD0000">"H\xf6me\";//需要察看是否支持此类文字编码<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
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");
<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> </font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%.2f\",3.14159);<br><br> printf(\"%10.2f\",3.14159);<br><br> printf(\"%.10f\",3.14159);<br><br> printf(\"%.9s\",abcdefghijklmn);<br><br> printf(\"%5.2f,%f,%7.3f\m\",3.14159,3.14159,3.14159);<br><br> printf(\"%b %d %f %s \n\",123,123,123,\"test\");<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
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 "
";<br> echo str_pad("这是标题",50," ",STR_PAD_BOTH)."\n";<br> foreach($index as $inkey=>$inval)<br> echo str_pad($inkey,30,".").str_pad($inval,20,".",STR_PAD_LEFT)."\n";<br> echo "
";
<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> </font><font color="#0000BB">$index </font><font color="#007700">= array(</font><font color="#DD0000">"one\"=>1,\"two\"=>155,\"three\"=>1679);<br><br> echo \"<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">\";<br><br> echo str_pad(\"这是标题\",50,\" \",STR_PAD_BOTH).\"\n\";<br><br> foreach($index as $inkey=>$inval)<br><br> echo str_pad($inkey,30,\".\").str_pad($inval,20,\".\",STR_PAD_LEFT).\"\n\";<br><br> echo \"</pre><div class="contentsignin">Nach dem Login kopieren</div></div>\";<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
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);<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> print </font><font color="#0000BB">strcmp</font><font color="#007700">(</font><font color="#DD0000">"aardvark\",\"aardwolf\");<br><br> print strncmp(\"aardvark\",\"aardwolf\",4);<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
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);<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> </font><font color="#0000BB">$var </font><font color="#007700">= </font><font color="#DD0000">"abcdefgh\";<br><br> print substr($var,2).\"\";<br><br> print substr($var,2,3).\"\";<br><br> print substr($var,-1).\"\";<br><br> print substr($var,-5,2).\"\";<br><br> print substr($var,-5,-2).\"\";<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
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);
////////////////////////////////////////////////////////////////////////
<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> </font><font color="#0000BB">$guest </font><font color="#007700">= </font><font color="#DD0000">"this is a string\";<br><br> $guestArray = explode(\" \",$guest);<br><br> var_dump($guestArray);<br><br> sort($guestArray);<br><br> echo implode(\",\",$guestArray);<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
5.替换字符和子字符串
string substr_replace(string source,string replace,int start[,int length])