Home  >  Article  >  Backend Development  >  Summary of 5 Tips for String Processing in PHP Programming_PHP Tutorial

Summary of 5 Tips for String Processing in PHP Programming_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:53:52738browse

字符串
注意单引号和双引号的区别
注意转义字符\的使用\\,\",\$
注意使用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
---------------------------------------------------------------------
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])//Return a string array
array implode( string glue,array pieces)//Return a string
/////////////////////////////Code segment////// ///////////////////////////////////
$guest = "this is a string";
$guestArray = explode(" ",$guest);
var_dump($guestArray);
sort($guestArray);
echo implode(",",$guestArray);
/// ////////////////////////////////////////////////////// ////////////////////
------------------------Output results----- ----------------------------------
array(4) { [0]=> string (4) "this" [1]=> string(2) "is" [2]=> string(1) "a" [3]=> string(6) "string" } a,is, string,this
-------------------------------------------------- --------------------------

5. Replacement characters and substrings

string substr_replace(string source, string replace,int start[,int length])

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318586.htmlTechArticleString Note the difference between single quotes and double quotes Note the use of escape characters\,",$ Note the use of 8 Hexadecimal or hexadecimal characters represent xf6 echo "Hxf6me";//You need to check whether such text is supported...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn