字符串操作与正则表达式

电子邮件

bool mail(string to, string subject, string message,
string [additional_headers [, string additional_parameters]]);
  • to 是邮件的目的地址
  • subject 是主题行
  • message 是消息内容
  • 第四个参数用来发送额外的、有效的邮件头(RFC822中有说明)
$additional_headers = "From: webserver@example.com\r\n" . 
	"Reply-To: bob@example.com";
  • 第五个参数可以向任何经过配置用来发送电子邮件的程序传递参数。

字符串格式化

字符串的整理

trim()

$name = trim($_POST['name']);
  • trim() 函数可以除去字符串开始和结束位置的空格,并将字符串返回
  • 默认过滤的字符 : \n(换行) \r(回车) \t(水平制表符) \x0B(垂直制表符) \0(字符串结束符) 和空格
  • 也可以通过添加第二个参数,增加新的过滤的特殊字符
  • ltrim() 和 rtrim() 分别从左边和右边去除空格

格式化字符串

n12br()

n12br($mailcontent);
  • 使用<br >代替换行符 ‘\n’ ;

为打印输出而格式化字符串

  • printf() 将一个格式化的字符串输出到浏览器中
  • sprintf() 返回衣蛾格式化了的字符串
echo "Total amount of order is $total";
printf("Total amount of order is %s", $total);
printf("Total amount of order is %.2f (with shipping %2f)", $total, $total_shipping);
  • 格式如下
  • %[ 'padding_character][ - ][ width ][ .precision ]type
    在这里插入图片描述
    在这里插入图片描述
// 也可以使用带序号的参数方式
printf("Total amount of order is %2\$.2f (with shipping %1\$.2f)", 
$total_shipping, $total);

字符串大小写

在这里插入图片描述

格式化字符串以便储存

  • 对于引号、斜杠和NULL需要进行转义处理,二部应该解释为控制符
  • " => " \ => \
$feedback = addslashes(trim($_POST['feedback']));
$feedback = stripslashes($feedback);

用字符串函数连接和分隔字符串

explode() 函数

array explode(string separator, string input [, int limit])
  • 根据指定的分隔符字符串将字符串分隔为小块,返回到数组中,通过limit可以限制分成小块的数量。
$email_array = explode('@' , $email);
$new_email = join('@', $email_array);
// or
$new_email = implode('@', $email_array);
  • implode() 和 join() 与explode() 函数效果相反;

strtok() 函数

string strtok(string input, string separator);

//example
$token = strtok($feedback, '"');
echo $token." <br \>";
while ($token != " "){
	$token = strtok( " ");
	echo $token." <br \>";
}
  • 该函数会保持内部指针在字符串中的位置,所以不用再次传递需要处理的字符串

substr() 函数

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

//example
$test = 'Your customer service is excellent';
substr($test, 1);
// result : "our customer service is excellent"
substr($test, -9);
// result : "excellent"
substr($test, 0, 4);
// result : "Your"
substr($test, 5, -13);
// result : "customer service"

字符串比较

字符串排序

strcmp(string str1, string str2);
  • str1 == str 2 return 0;
  • str1 > str 2 return 1;
  • str1 < str 2 return -1;
  • strcasecmp() 函数和strcmp() 一样,只是不区分大小写;
  • strnatcmp() 自然排序;

字符串长度

if (strlen($email) < 6){
	echo 'That email adress is not valid';
	exit;
}

使用字符串函数匹配和替换子字符串

字符串中查找字符串

string strstr(string haystack, string needle);
  • strstr() 函数和 strchr() 函数一样
  • 如果找到目标关键字的精确匹配,函数从目标关键字前面返回被搜索的字符串,否则返回false
//example
$toaddress = "feedback@example.com"

if(strstr($feedback, 'shop'))
	toaddress = "retail@example.com";
elseif (strstr($feedback, 'delivery'))
	...
  • stristr() 函数不区分大小写
  • strrchr() 从最后出现目标关键字的位置的前面返回被搜索的字符串

查找子字符串

int strpos(string haystack, string needle, int [offset]);
  • strpos() 比 strstr() 速度更快
  • 返回被搜索字符串中第一个出现目标关键字子字符串的位置
//example
$test = "Hello world";
echo strpos($test, 'o');
// result : 4
echo strpos($test, 'o', 5)
//result: 7
  • offset 可以制定被搜索字符串的开始搜索位置
  • strrpos() 返回被搜索字符串中最后一次出现目标关键字子字符串的位置
  • 如果不存在,返回false

替换子字符串

mixed str_replace(mixed needle, mixed new_needle, mixed haystack[, int&count])
  • 可以以数组的方式传递所有参数
//example
$feedback = str_replace($offcolor , '%!@*', $feedback);
string substr_replace(string string, string replacement, 
						int start, int [length]);
  • 用replacement 替换string 中的一部分,由start和length制定
//example
$test = substr_replace($test, 'X', -1);
  • length 表示php停止操作的位置,若不给定值,则会从start开始到字符串结束
  • 如果length为0,替换字符串会插入到字符串中。
  • 正length表示新字符串替换掉的字符串长度
  • 负length表示到字符串尾部倒数第几个停止替换。

正则表达式

  • POSIX 正则表达式易掌握,但是不是二进制安全的

字符集和类

  • 用字符作为通配符代替除换行符(\n)以外的任一个字符
  • .at 可以与 “cat” “sat” “mat” “#at” 匹配
  • [a-z]at
  • [aeiou]
  • [a-zA-Z]
  • [^a-z] 不匹配a-z
    在这里插入图片描述

重复

  • [[:alnum:]]+
  • *表示这个模式可以被重复0或更多次
  • +表示这个模式可以被重复1或更多次
  • 符号放在表达式的后面

子表达式

  • (very )*large
  • 匹配"large" “very large” “very very large”

子表达式计数

  • (very ){1, 3}
  • 在花括号中指定内容允许重复的次数

定位到字符串的开始或末尾

  • ^bob 在字符串开始处匹配bob
  • com$ 在字符串末尾处匹配com
  • ^[a-z]$ 匹配只包含a-z之中一个字符的字符串

分支

  • com|edu|net 表示com 或edu 或net

匹配特殊字符

  • 需要增加 \
  • 若果要用$ 需要用"\\$" PHP 解释器解析为 \$ 正则表达式 解释为$
    在这里插入图片描述

电子邮件

  • ^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$
  • 当字符类的开始或者末尾处使用点号时,点号失去其特殊通配符的意义,只能成为一个点号字符

用正则表达式查找字符串

int ereg(string pattern, string search, array[matches]);
  • eregi() 不区分大小写
//example
if (!eregi('^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+$', $email)){
	echo '...';
	exit;
}
$toaddress = "feedback@example.com";
if (eregi("shop|customer service|retail", $feedback))
	$toaddress = "retail@example.com";

用正则表达式替换子字符串

string ereg_replace(string pattern, string replacement, string search);
  • 在search中查找字符串,用replacement 代替pattern
  • eregi_replacement() 不区分大小写

正则表达式分割字符串

array split(string pattern, string search [, int max]);
  • 把search 分割成符合正则表达式的字符串,然后将字符串返回到数组中,max指定进入数组的元素个数
// example
$address = "username@example.com"
$arr = split('\.|@', $address);
while(list($key, $value) = each($arr)){
	echo "<br />".$value;
}
  • 一般而言,正则表达式运行效率要低于字符串函数。如果应用程序足够简单,那么就用字符串表达式。但是,对于可以通过单个正则表达式执行的任务来说,如果使用多个字符串函数,则是不对的。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值