Chinaunix首页 | 论坛 | 博客
  • 博客访问: 53470
  • 博文数量: 17
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 205
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-15 12:22
文章分类
文章存档

2011年(1)

2008年(16)

我的朋友
最近访客

分类:

2008-08-26 22:14:06


2008-08-01 22:15

6-6.php



去除字符串两端的空格


$str=" 你看不到我 我是空格 ";
echo "方括号中为原始字符串:[".$str."]
";
echo "原始字符串长度:".strlen($str)."
";
$str1=ltrim($str);
echo "执行ltrim()之后的长度:".strlen($str1)."
";
$str2=rtrim($str);
echo "执行rtrim()之后的长度:".strlen($str2)."
";
$str3=trim($str);
echo "执行trim()之后的长度:".strlen($str3)."
";
echo "去掉首尾空格之后的字符串:[".$str3."]";
?>


===============================================================

6-7.php



字符串处理中的单词处理


$str="ni hao , wo jiao Wang Xiao-ming.";
echo "原始字符串:".$str."
";
$str1=ucfirst($str);
echo "执行ucfirst()之后:".$str1."
";
$str2=ucwords($str);
echo "执行ucwords()之后:".$str2."
";
$str3=strtoupper($str);
echo "执行strtoupper()之后:".$str3."
";
$str4=strtolower($str);
echo "执行strtolower()之后:".$str4."
";
echo "字符串中一共有:".str_word_count($str)."个单词.";
?>


===============================================================

6-8.php



用substr_count()函数统计字符串出现次数


$str="I am an abstract about abroad.";
echo substr_count($str,"ab");
echo ", ";
echo substr_count($str,"ab",6,4);
?>


===============================================================

6-9.php



字符串查找函数的使用


$str="I am an abstract about abroad.";
echo "原始字符串为:".$str."
";
echo "ab在字符串中的第一次出现位置为:".strpos($str,"ab")."
";
echo "ab在字符串中的最后一次出现位置为:".strrpos($str,"ab")."
";
echo "abcd在字符串中第一次出现的位置为:".strpos($str,"abcd");
?>


===============================================================

6-10.php



字符串查找函数的使用(二)


$str="千山鸟飞绝,万径人踪灭,孤舟原理翁,独钓寒江雪。";
echo "1.原始字符串为:".$str."
";
echo "用strstr函数搜索\",\"的返回结果:".strstr($str,",")."
";
echo "用strstr函数搜索\"孤舟\"的返回结果:".strstr($str,"孤舟")."
";
$str2="I have a great dream.";
echo "2.原始字符串为:".$str2."
";
echo "用strstr函数搜索\"e\"的返回结果:".strstr($str2,"e")."
";
echo "试图用strrchr函数搜索\"at\"的返回结果:".strrchr($str2,"at");
?>


===============================================================

6-11.php



nl2br()函数的使用



请输入一段包含回车的文字:




$content=$_POST["content"];
//如果用户输入内容不为空
if($content!="")
{
echo "
";
echo "直接输出接受到的内容:
";
echo $content;
echo "
(内容长度:".strlen($content).")
";
echo "
";
echo "用nl2br()处理接收到的内容,然后输出:
";
echo nl2br($content);
echo "
(内容长度:".strlen(nl2br($content)).")
";
}
?>


===============================================================

6-12.php



字符串替换函数的使用


//单个字符替换
$str="当所有的人[逗]离开我的时候[逗]你劝我要耐心等候[句]";
echo "原字符串:".$str."
";
$str=str_replace("[","(",$str);
$str=str_replace("]",")",$str);
echo "字符替换之后:".$str."
";
//字符串替换
$str=str_replace("(逗)",",",$str);
$str=str_replace("(句)",".",$str);
echo "字符串替换之后:".$str."
";
?>


===============================================================

6-13.php



字符串替换的高级应用


//单个字符替换
$str="当所有的人[逗]离开我的时候[逗]你劝我要耐心等候[句]";
echo "原始字符串:".$str."
";
$arr1=array("[","]");
$arr2=array("(",")");
$str=str_replace($arr1,$arr2,$str);
echo "字符替换之后:".$str."
";
//字符串替换
$arr3=array("(逗)","(句)");
$arr4=array(",",".");
$str=str_replace($arr3,$arr4,$str);
echo "字符串替换之后:".$str."
";
?>


===============================================================

6-14.php



字符串的截取综合范例


//构造字符串
$str="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
echo "原始字符串:".$str."
";
//按各种方式进行截取
$str1=substr($str,5);
echo "从第5个字符开始取至最后:".$str1."
";
$str2=substr($str,9,4);
echo "从第9个字符开始取4个字符:".$str2."
";
$str3=substr($str,-5);
echo "取倒数5个字符:".$str3."
";
$str4=substr($str,-8,4);
echo "从倒数第8个字符开始向后取4个字符:".$str4."
";
$str5=substr($str,-8,-2);
echo "从倒数第8个字符开始取到倒数第2个字符为止:".$str5."
";
?>


===============================================================

6-15.php



explode字符串分割函数


//构造字符串
$str="苹果,空心菜,香蕉,萝卜,大蒜,牛肉";
echo "原字符串:".$str."
";
echo "1.以逗号为分隔符分割字符串:
";
$arr1=explode(",",$str);
echo "---\$arr1[0]的值:".$arr1[0]."
";
echo "---\$arr1[4]的值:".$arr1[4]."
";
echo "2.分割时指定limit参数:
";
$arr2=explode(",",$str,3);
echo "---\$arr2[0]的值:".$arr2[0]."
";
echo "---\$arr2[2]的值:".$arr2[2]."
";
echo "---\$arr2[4]的值:".$arr2[4]."
";
?>


===============================================================

6-16.php



str_split字符串分割函数


//分割英文字符串
$str="Quietly I leave,just as quietly I came.";
echo "原字符串:".$str."
";
echo "1.以默认长度分割字符串:
";
$arr1=str_split($str);
echo "---\$arr1[0]的值:".$arr1[0]."
";
echo "---\$arr1[1]的值:".$arr1[1]."
";
echo "---\$arr1[10]的值:".$arr1[10]."
";
echo "2.以指定长度为5分割字符串:
";
$arr2=str_split($str,5);
echo "---\$arr1[0]的值:".$arr2[0]."
";
echo "---\$arr1[1]的值:".$arr2[1]."
";
echo "---\$arr1[5]的值:".$arr2[5]."
";
//测试分割中文
$str2="轻轻地我走了,正如我轻轻的来。";
echo "原字符串:".$str2."
";
echo "1.以指定长度为5分割字符串:
";
$arr3=str_split($str2,5);
echo "---\$arr3[0]的值:".$arr3[0]."
";
echo "---\$arr3[1]的值:".$arr3[1]."
";
echo "2.以指定长度为4分割字符串:
";
$arr4=str_split($str2,4);
echo "---\$arr4[0]的值:".$arr4[0]."
";
echo "---\$arr4[0]的值:".$arr4[0]."
";
echo "---\$arr4[0]的值:".$arr4[0]."
";
?>


===============================================================

File create time:2008-08-01 22:14:49
By string.cpp

阅读(704) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:PHP数组函数

给主人留下些什么吧!~~