今天的学习内容是:正则表达式,由于最近很忙,所以对代码的解释做的不怎么好,以后有时间我将重新改一下
字串3
/* 字串2
//////////////////////////////////////
author:yangfan
date :2008/03/13
正则表达式!
*/ 字串5
//字符串格式化
//Strtoupper将字符串变成大写 Strtolower将字符串变成小写 Ucfirst将第一个字母变成大写 Ucwords将字符串的每一个单词变成大写
字串7
$name = " yangfan ";
$name1 = trim($name); //trim清除字符串前后的空格
echo $name1;
字串8
// addslashes 转意 在字符串加入数据库前应该用到!
echo addslashes("fadfa'fads'");
字串6
//explode分割字符串
$mail = "
332443141@qq.com";
$mail_array = explode(
'@',$mail);
echo '
'.$mail_array[0];
echo '
'.$mail_array[1].'
';
字串3
//implode、join连接字符串
echo implode(
'@',$mail_array).''; 字串4
//strtok函数
$string = "This is\tan example\nstring";
$tok = strtok($string, " \n\t");
while ($tok) {
echo strlen($tok).'kb=>';
echo "Word=$tok
";
$tok = strtok(" \n\t");
}
字串2
//substr()函数
$test = 'this is
www.cnjiaocheng.com!';
echo substr($test,5).'
';
echo substr($test,0,4).'
';
echo substr($test,5,-10).'
';
字串6
//strcmp 用语比较2个字符串 strcasecmo除了不出分大小外其他与strcmp相同!
$str1 = "aaa";
$str2 = "bbb";
echo strcmp($str1,$str2).'
';//如果输出的负数,说明:str1
字串4
//strlen()函数测试字符串的长度
echo strlen("
www.cnjiaocheng.com").'';
字串3
//strstr()在字符串中查找字符串
$str1 = "
www.cnjiaocheng.com";
$str2 = "www";
if (strstr($str1,$str2)){
echo "ok
";
}
字串6
//查找字符串的位置strpos()函数可以实现
$test1 = 'hello world';
echo strpos($test1,'l').'
';
echo strpos($test1,'o',5);//指定从第5个单词开始搜索
$result = strpos($test1,'k');
if($result === false)
echo '
Not found';
else
echo '
Found';
字串3
//替换字符串
$offcolor = array('fuck','good');
$feedback = "you are a good man!";
$feedback = str_replace($offcolor,'***',$feedback);//替换的单词库,替换后的内容,需要替换的字符串
echo '
'.$feedback; 字串8
$test3 = "
www.cnjiaocheng.com";
echo '
'.substr_replace($test3,'xx',-10);//www.cnjiaxx
字串8
/*////////////////////////////////////////
正则表达式的介绍
////////////////////////////////////////*/
echo '
';
$email = "
3@qq.com";
if(!eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$',$email)){
echo "That is not a valid email address";
exit();
}
else{
echo "good!pass!";
}
if(eregi('qq|163|sina',$email)){
echo '
'.$email." come from qq-email";
}
if(eregi('qq\.com',$email)){
echo '
qq.com';
}
字串1
echo "
";
字串5
//正则替换
$string = "This is a test";
echo ereg_replace (" is", " was", $string).'
';
echo ereg_replace ("( )is", "
\\1was", $string).'
';
echo ereg_replace ("(( )is)", "
\\2was", $string).'
'; 字串1
//正则分割字符串
$address =
'0hudu@163.com';
$add = split('\.|@',$address);
while(list($key,$value) = each($add))
echo '
'.$key.'-'.$value;
?>
字串4