Presentation is loading. Please wait.

Presentation is loading. Please wait.

第12章 字符串处理.

Similar presentations


Presentation on theme: "第12章 字符串处理."— Presentation transcript:

1 第12章 字符串处理

2 12.1 格式化字符串 1、整理字符串 函数trim()用来整理输入的数据,除去字符串首部和尾部的空白。例如:$name=trim($name); 函数ltrim()是只从开始处(左边首部)除去空白。 函数rtrim()是只从尾部(右边尾部)除去空白。

3 2、格式化字符串以便显示 nl2br()函数将字符串作为参数,用HTML中的<BR>标签代替字符串中的换行符。
为输出而格式化字符串。print()完成与echo一样的事情,但它有返回值。pringf(),sprintf()按照一定的格式输出。 例如:printf(“总价值是%.2f元”,$total);

4 类型 意 义 b 作为二进制数输出 c 作为字符输出 d 作为整数输出 f 作为浮点数输出 o 作为八进制数输出 s 作为字符串输出 x 作为带有小写字母a-f的十六进制数输出 X 作为带有大写字母A-F的十六进制数输出

5 改变字符串中的字母的大小写 将字符串转换为大写strtoupper() 将字符串转换为小写strtolower() 函数 用法 返回值 $subject Feedback strtoupper() strtoupper($subject) FEEDBACK strtolower() strtolower($subject) feedback

6 3、格式化字符串以便存储 在将字符写到数据库前,应该用AddSlashes()将它们重新格式化,例如: $feedback=AddSlashes($feedback); 在检索字符串的时候,需要将斜杠除去,可以用函数StripSlashes(),例如: $feedback=StripSlashes($feedback);

7 12.2 用字符串函数连接和分割字符串 1、使用函数explode()、implode() array explode(string separator, string input); $ _array); 2、使用strtok() string strtok(string input, string separator);

8 3、使用substr() string substr(string string,int start[, int length]);
函数substr()可以访问一个给定起点和终点的子串。注意字符串的起点和数组一样是从零开始的。 例如:$test=“Your service is good”; 长度参数可以用于指定是字符串序列的尾部(是负的)。substr($test,5,-8);返回从第6个字符到倒数第8个字符,就是“service”。 如果只有一个负数作为起点,将得到一个原字符串尾部的一个子串,字符个数等于给定负数的绝对值。substr($test,-4);返回“good”。 如果只有一个正数作为起点,将得到从起点到终点的整个字符串。substr($test,1);只用一个正数作为起点调用它,返回“our service is good”。 长度参数可以用于指定返回字符的个数(是正的)。substr($test,0,4);返回字符串的头四个字符

9 12.3 比较字符串 1、字符串比较 strcmp(str1,str2) strcasecmp(str1,str2)——不区分大小写 strnatcmp(str1,str2) ——自然排序算法 2、用strlen()测试字符串的长度

10 12.4 用字符串函数匹配和替换子串 1、在字符串中查找字符串 strstr(str,substr) strchr()—strstr()的别名函数 strrchr(str,substr) stristr(str,substr) 2、查找子串的位置 strpos(str,substr) strrpos(str,substr) 3、替换子串 str_replace(search,replacement,str) substr_replace(str,replacement,start,length)


Download ppt "第12章 字符串处理."

Similar presentations


Ads by Google