一,大括号的使用
说明,为了区分变量和字符串,
- <?
- $a="basket";
- $b="i will play {$a}ball in the summertime!!";
- ?>
输出结果 i will play basketball in the summertime!!
为了区分变量$a和ball不是变量$aball
二,字符串的索引 和数组一样,字符串也可以通过索引输出
- <?
- header("Content-Type:text/html; charset=utf-8");
- $a="hello my php!!";
- for ($i=0;$i<15;$i++)
- {
- echo $a[$i]."<br>";
- }
- ?>
输出结果
h
e
l
l
o
m
y
p
h
p
!
!
三 字符串的连接符,.和.=
- <?
- header("Content-Type:text/html; charset=utf-8");
- $a="hello my php!!";
- $b="i will give you a job!!";
- $a.=$b;
- echo $a;
- ?>
输出结果 hello my php!!i will give you a job!!
四,串联字符串
$变量名=<<<开始表示
字符串
结束表示;
- <?
- header("Content-Type:text/html; charset=utf-8");
- $a=<<<sql
- select * from mysql
- where channl_id=0 order
- by channl_id desc
- sql;
- echo $a;
- ?>
输出结果 select * from mysql where channl_id=0 order by channl_id desc
输出echo print printf print_r
- <?
- header("Content-Type:text/html; charset=utf-8");
- $a=12.3456;
- printf('%.2f',$a);
- ?>
输出结果 12.35
获取字符串的长度 strlen
语法 strlen($变量名)
- <?
- header("Content-Type:text/html; charset=utf-8");
- $a="welcome to php";
- $n=strlen($a);
- for($i=0;$i<$n;$i++)
- {
- echo $a[$i]."<br>";
- }
- ?>
去除首尾字符
trim ltrim rtrim
trim($变量名,"被去除的字符串")
- <?
- header("Content-Type:text/html; charset=utf-8");
- $action=$_REQUEST['action'];
- $a1=trim($_POST['a1'],";");
- if($action=='tj')
- {
- echo $a1;
- }
- ?>
- <form action="" name="form1" method="post">
- <input name="a1" type="text" />
- <input name="action" type="hidden" value="tj" />
- <input name="bt" type="submit" value="提交"/>
- </form>
输出结果请下载执行
改变字符串大小写
共四个参数 变为小写strtolower 变为大写strtoupper 首字母大写ucfirst 每个词首字母大写ucwords
语法 strtolower($a) strtoupper($a) ucfirst($a) ucwords($a)
- <?
- header("Content-Type:text/html; charset=utf-8");
- $action=$_REQUEST['action'];
- $a1=ucwords($_POST['a1']);
- if($action=='tj')
- {
- echo $a1;
- }
- ?>
- <form action="" name="form1" method="post">
- <input name="a1" type="text" />
- <input name="action" type="hidden" value="tj" />
- <input name="bt" type="submit" value="提交"/>
- </form>
在字符串中查找 substr
语法 substr(查找的字符串,开始位置,从开始位置起多少字符)
- <?
- header("Content-Type:text/html; charset=utf-8");
- $st="mysql php .net";
- $str=substr($st,2,5);
- echo $st."<br>";
- echo $str;
- ?>
输出结果
mysql php .net
sql p
查找在字符串中出现的第一次的位置,和最后一次出现的位置
语法 strpos(欲查找的字符串,查找的元素) strrpos(欲查找的字符串,查找的元素)
- <?
- header("Content-Type:text/html; charset=utf-8");
- $a="welcome to php,php is so easy!!";
- $b=strpos($a,'php');
- $c=strrpos($a,'php');
- echo $b."<br>";
- echo $c;
- ?>
输出结果
11
15
五,分解字符串
explode 语法
explode('以什么特征为依据',欲分解的字符串)
- <?
- header("Content-Type:text/html; charset=utf-8");
- $a="asp,php,asp.net";
- $b=explode(',',$a);
- print_r($b);
- ?>
输出结果
Array ( [0] => asp [1] => php [2] => asp.net ) 说明:以逗号为分割依据,把字符串分割成数组
获取字符串分解后最后一个 end(explode())
语法
end(explode('以什么特征为依据',欲分解的字符串))
多用于截取文件名
- <?
- header("Content-Type:text/html; charset=utf-8");
- $filename="/Uplodefile/new/2009/02/18/12606.168.24.jpg";
- $b=end(explode('/',$filename));
- echo $b;
- ?>
输出结果 12606.168.24.jpg
按照自定义个数截取字符串 str_split
语法 str_split(要截取的字符串,截取多少个字符)
- <?
- header("Content-Type:text/html; charset=utf-8");
- $string="hello world.the day is a beautiful day.";
- $b=str_split($string,3);
- foreach ($b as $v)
- {
- echo $v."<br>";
- }
- ?>
输出结果
hel
lo
wor
ld.
the
da
y i
s a
be
aut
ifu
l d
ay.
六,将数组转化成字符串 implode
语法 implode(以什么分割,$欲转换的数组)
- <?
- header("Content-Type:text/html; charset=utf-8");
- $arr=array('张超','赵永峰','郭磊','王晨');
- $b=implode(',',$arr);
- echo $b.'<br>';
- var_dump($b);
- ?>
输出结果
张超,赵永峰,郭磊,王晨
string(30) "张超,赵永峰,郭磊,王晨"
七。替换字符串 str_replace
语法 str_replace('查找要替换的值','替换后的值',替换的字符串)
- <?
- header("Content-Type:text/html; charset=utf-8");
- $content=$_POST['content'];
- $con1=str_replace('张超','方头怪人',$content);
- echo $con1.'<br>';
- ?>
- <form name="form1" action="" method="post">
- <textarea name="content" cols="" rows=""></textarea>
- <input name="action" type="hidden" value="tj" />
- <input name="" type="submit" value="提交" />
- </form>
八,加密函数 md5
语法md5(欲加密的字符串)
- <?
- header("Content-Type:text/html; charset=utf-8");
- $content=$_POST['content'];
- $con1=md5($content);
- echo $con1.'<br>';
- ?>
- <form name="form1" action="" method="post">
- <textarea name="content" cols="" rows=""></textarea>
- <input name="action" type="hidden" value="tj" />
- <input name="" type="submit" value="提交" />
- </form>
















