一,大括號的使用
說明,爲了區分變量和字符串,
- <?
- $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>