php 字符串处理

来源:互联网 发布:富士康网络重复报名 编辑:程序博客网 时间:2024/06/08 17:27

一 字符串格式化

1.trim()   可以出去字符串开始位置和结束位置的空格,并返回结果字符串

ltrim()   除去左边空格

rtrim()  除去右边空格

2.nl2br()

用<br />代替字符串中的换行符

3.printf() 将一个格式化的字符串输出到浏览器中

sprintf() 返回一个格式化的字符串

例:printf("Total amount of order is %.2f",$total);

转换说明所遵循的格式

%['填充字符][-][width][.precision]type

4.大小写

strtoupper()

strtolower

Ucfirst() 如果字符串的第一个字符是字母,则转为大写

Ucwords() 将每个单词的第一个字母转换为大写

5.格式化字符串以便存储

addslashes() 

stripslashes()


二.连接和分割字符串

1.explode()分割 函数原型  array explode(string separator, string input,[,int limit]);

例: $email_array=explode('@',$email);

2.implode()和join()连接

例:$new_email=implode('@',$email_array);

3.strtok() 一次只从字符串中取出一些片段(成为令牌)    函数原型 string strok(string input, string separator);

4.

string substr ( string $string , int $start [, int $length ] )

返回字符串 string 由 start 和 length 参数指定的子字符串。

start为正数  得到从起点到字符串结束的整个字符串

start为负数  得到字符串尾部的一段字符串,字符个数等于给定负数的绝对值

<?php
$rest 
substr("abcdef", -1);    // 返回 "f"
$rest substr("abcdef", -2);    // 返回 "ef"
$rest substr("abcdef", -31); // 返回 "d"
?>


length为正数  字符的个数

length为负数 字符串序列的尾部

<?php
$rest 
substr("abcdef"0, -1);  // 返回 "abcde"
$rest substr("abcdef"2, -1);  // 返回 "cde"
$rest substr("abcdef"4, -4);  // 返回 ""
$rest substr("abcdef", -3, -1); // 返回 "de"
?>


三. 字符串的比较

strcmp()

strcasecmp()
strnatcmp()

strnatcasecmp()

strlen()


四、 字符串函数匹配和替换子字符串

strstr()

stristr()

strchr()

strrchr()

strpos()
strrpos()

str_replace()

substr_replace()

0 0
原创粉丝点击