PHP基本語法第四章-字符串的處理

一,大括號的使用

說明,爲了區分變量和字符串,

  1. <?  
  2. $a="basket";  
  3. $b="i will play {$a}ball in the summertime!!";  
  4. ?> 

輸出結果 i will play basketball in the summertime!!

爲了區分變量$a和ball不是變量$aball

二,字符串的索引 和數組一樣,字符串也可以通過索引輸出

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3.  
  4. $a="hello my php!!";  
  5. for ($i=0;$i<15;$i++)  
  6. {  
  7.     echo $a[$i]."<br>";  
  8. }  
  9. ?> 

輸出結果

h
e
l
l
o

m
y

p
h
p
!
!
三 字符串的連接符,.和.=

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3.  
  4. $a="hello my php!!";  
  5. $b="i will give you a job!!";  
  6. $a.=$b;  
  7. echo $a;  
  8. ?> 

輸出結果 hello my php!!i will give you a job!!

四,串聯字符串

$變量名=<<<開始表示

字符串

結束表示;

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3.  
  4. $a=<<<sql  
  5. select * from mysql   
  6. where channl_id=0 order  
  7. by channl_id desc  
  8. sql;  
  9. echo $a;  
  10. ?> 

輸出結果 select * from mysql where channl_id=0 order by channl_id desc

輸出echo print printf print_r

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3.  
  4. $a=12.3456;  
  5. printf('%.2f',$a);  
  6.  
  7. ?> 

輸出結果 12.35

獲取字符串的長度 strlen

語法 strlen($變量名)

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3.  
  4. $a="welcome to php";  
  5. $n=strlen($a);  
  6. for($i=0;$i<$n;$i++)  
  7. {  
  8.     echo $a[$i]."<br>";  
  9. }  
  10.  
  11. ?> 

去除首尾字符

trim ltrim rtrim

trim($變量名,"被去除的字符串")

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3. $action=$_REQUEST['action'];  
  4. $a1=trim($_POST['a1'],";");  
  5. if($action=='tj')  
  6. {  
  7.     echo $a1;  
  8. }  
  9. ?>  
  10. <form action="" name="form1" method="post">  
  11. <input name="a1" type="text" />  
  12. <input name="action" type="hidden" value="tj" />  
  13. <input name="bt" type="submit" value="提交"/>  
  14. </form> 

輸出結果請下載執行

改變字符串大小寫

共四個參數 變爲小寫strtolower 變爲大寫strtoupper 首字母大寫ucfirst 每個詞首字母大寫ucwords

語法 strtolower($a) strtoupper($a) ucfirst($a) ucwords($a)

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3. $action=$_REQUEST['action'];  
  4. $a1=ucwords($_POST['a1']);  
  5. if($action=='tj')  
  6. {  
  7.     echo $a1;  
  8. }  
  9. ?>  
  10. <form action="" name="form1" method="post">  
  11. <input name="a1" type="text" />  
  12. <input name="action" type="hidden" value="tj" />  
  13. <input name="bt" type="submit" value="提交"/>  
  14. </form> 

 在字符串中查找 substr

語法 substr(查找的字符串,開始位置,從開始位置起多少字符)

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3. $st="mysql php .net";  
  4. $str=substr($st,2,5);  
  5. echo $st."<br>";  
  6.     echo $str;  
  7.  
  8. ?> 

輸出結果

mysql php .net
sql p

查找在字符串中出現的第一次的位置,和最後一次出現的位置

語法 strpos(欲查找的字符串,查找的元素) strrpos(欲查找的字符串,查找的元素)

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3. $a="welcome to php,php is so easy!!";  
  4. $b=strpos($a,'php');  
  5. $c=strrpos($a,'php');  
  6. echo $b."<br>";  
  7.     echo $c;  
  8.  
  9. ?>  

輸出結果

11

15

五,分解字符串

explode 語法

explode('以什麼特徵爲依據',欲分解的字符串)

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3. $a="asp,php,asp.net";  
  4. $b=explode(',',$a);  
  5. print_r($b);  
  6.  
  7. ?> 

輸出結果

Array ( [0] => asp [1] => php [2] => asp.net )  說明:以逗號爲分割依據,把字符串分割成數組

獲取字符串分解後最後一個 end(explode())

語法

end(explode('以什麼特徵爲依據',欲分解的字符串))

多用於截取文件名

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3. $filename="/Uplodefile/new/2009/02/18/12606.168.24.jpg";  
  4. $b=end(explode('/',$filename));  
  5. echo $b;  
  6.  
  7. ?>  

輸出結果 12606.168.24.jpg

按照自定義個數截取字符串 str_split

語法 str_split(要截取的字符串,截取多少個字符)

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3. $string="hello world.the day is a beautiful day.";  
  4. $b=str_split($string,3);  
  5. foreach ($b as $v)  
  6. {  
  7.     echo $v."<br>";  
  8. }  
  9.  
  10. ?>  

輸出結果

hel
lo
wor
ld.
the
da
y i
s a
be
aut
ifu
l d
ay.
六,將數組轉化成字符串 implode

語法 implode(以什麼分割,$欲轉換的數組)

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3. $arr=array('張超','趙永峯','郭磊','王晨');  
  4. $b=implode(',',$arr);  
  5. echo $b.'<br>';  
  6. var_dump($b);  
  7. ?>  

輸出結果

張超,趙永峯,郭磊,王晨
string(30) "張超,趙永峯,郭磊,王晨"

七。替換字符串 str_replace

語法 str_replace('查找要替換的值','替換後的值',替換的字符串)

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3. $content=$_POST['content'];  
  4. $con1=str_replace('張超','方頭怪人',$content);  
  5. echo $con1.'<br>';  
  6. ?>  
  7. <form name="form1" action="" method="post">  
  8. <textarea name="content" cols="" rows=""></textarea>  
  9. <input name="action" type="hidden" value="tj" />  
  10. <input name="" type="submit" value="提交" />  
  11. </form> 

八,加密函數 md5

語法md5(欲加密的字符串)

  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3. $content=$_POST['content'];  
  4. $con1=md5($content);  
  5. echo $con1.'<br>';  
  6. ?>  
  7. <form name="form1" action="" method="post">  
  8. <textarea name="content" cols="" rows=""></textarea>  
  9. <input name="action" type="hidden" value="tj" />  
  10. <input name="" type="submit" value="提交" />  
  11. </form> 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章