字符串處理

1、輸出字符串
     1. echo
       void echo ( string arg1 [, string ...] ) 是一個語法 ,不是函數
       echo 沒有返回值;
       echo 能夠輸出多個值,使用逗號分隔;
       
       $val = "world" ;
       echo "hello" , $val ;
 
     2. print
       int print ( string arg )  實際上不是一個函數(它是一個語言結構)
 
        print ( "hello $val" );
        print "hello world" ;
 
     3.printf() 
       功能:格式化輸出字符串;
       int printf ( string format [, mixed args [, mixed ...]] )
          
         %b二進制輸出         //brianry
         %d整數輸出           //data
         %f浮點數輸出         //float
         %s字符串輸出         //string
 
         $str = "123 test" ;
         printf( "整數:%d" , $str );
         printf( "浮點數:%.2f" , $str );
         printf( "字符串:%s" , $str );
 
     4.sprintf()
 
       功能與printf相同,但不會直接輸出結果;
 
       string sprintf ( string format [, mixed args [, mixed ...]] )
 
       $str = "123 test" ;
       $val = sprintf( "%d" , $str );
       echo $val ;
 
 
   2、查找與替換
 
       1. strpos ()
         int strpos ( string haystack, mixed needle [, int offset] )
 
         strpos ()函數在 haystack 中以區分大小寫的方式找到 needle 第一次出現的位置;
         若是沒有找到則返回FALSE;可選參數offset 指定開始查找的位置;
 
 
         echo strpos ( "Hello world!" , "wo" );
 
 
       2. stripos ()
         stripos ()與 strpos ()功能相同,只是查找時不區別大小寫;
         
 
 
       3. str_replace ()
         mixed str_replace ( mixed search, mixed replace, mixed subject [, int & count ] )
 
         str_replace ()函數在subject中以區分大小寫的方式搜索 search ,用replace替換找到的全部內容;
         若是沒有找到search,則subject保持不變;
         若是定義了可選參數 count 則只替換subject中 count 個search
 
         $str = "test@163.com" ;
         $email = str_replace ( "@" , "(at)" , $str );
         echo $email ;
 
 
       4. str_ireplace ()
         str_ireplace ()與 str_replace ()功能相同,只是不區分大小寫;
 
 
   3、截取字符串
 
       1. substr ()
       string substr ( string string, int start [, int length] )
 
       從start位置取出length長度的字符,字符串位置開始值爲零;若是沒有指定length,那麼默認一直到字符串末尾;
 
 
       echo substr ( "Hello world" , 6);
       echo substr ( "hello world" , 6, 5);
 
 
       2. strstr ()
 
         string strstr ( string haystack, string needle )
 
         strstr () 函數搜索一個字符串在另外一個字符串中的第一次出現。該函數返回字符串的其他部分(從匹配點)。
         若是未找到所搜索的字符串,則返回false
 
         echo strstr ( "Hello world!" , "world" );
 
 
         3. stristr ()
 
         stristr ()與 strstr ()功能相同,只是不區分大小寫;
 
         echo strstr ( "Hello world!" , "WORLD" );
 
 
 
 
   4、刪除字符串
       1.ltrim()
 
         string ltrim ( string str [, string charlist] )
         ltrim 函數刪除字符串左側空格或其餘預約義字符;
 
         若是未設置charlist參數,則刪除如下字符:
         "\0"  NULL
         "\t"     製表符
         "\n"     換行
         "\x0B"   垂直製表符
         "\r"     回車
         " "      空格
 
         $str = "       Hello World!" ;
         echo ltrim( $str );
 
 
       2.rtrim()
 
         string rtrim ( string str [, string charlist] )
         rtrim 函數刪除字符串右側空格或其餘預約義字符;
 
 
       3.trim()
 
         trim 函數刪除字符串兩側空格或其餘預約義字符;
 
 
   5、其它字符串處理函數
 
       1. strlen () 獲取字符串長度
 
         $passwd = "123456" ;
           if ( strlen ( $passwd ) < 8){
             echo "密碼不能少於8位" ;
           }
 
       2. strtolower () 將字符串轉換爲小寫字母
 
           $url = "HTTP://WWW.WENGDO.COM/ " ;
           echo strtolower ( $url );
 
 
       3. strtoupper () 將字符串轉換爲大寫字母
           $str = "中文 hello world" ;
           echo strtoupper ( $str );
 
       4. strrev () 反轉字符串
           $str = "hello world" ;
           echo strrev ( $str );
 
       5. nl2br () 將字符串中換行 (\n) 轉換成 HTML 換行標籤 (<br>)
           $str = "hello
           world";
           echo nl2br ( $str );
 
 
       6. strip_tags () 刪除字符串中HTML XML PHP 標籤
         string strip_tags ( string str [, string allowable_tags] )
         可選參數 allowable_tags 指定要保留的標籤;
 
         $str = "test <a href=" http: //www.163.com">163</a>";
         echo strip_tags ( $str );
 
 
     7. htmlspecialchars() 函數把一些預約義的字符轉換爲 HTML 實體
 
         預約義的字符是:
         & (和號)   成爲  &
         " (雙引號) 成爲  "
         ' (單引號) 成爲  '
         < (小於)   成爲  <
         > (大於)   成爲  >
 
         $str = "<p> 這是一個段落 </p>" ;
         echo htmlspecialchars( $str );
 
 
     練習:
 
       1.如下代碼運行的結果?
         $a = "PHPlinux" ;
         $b = "PHPLinux" ;
         $c = strstr ( $a , "L" );
         $d = stristr ( $b , "l" );
         echo $c . "is" . $d ;
         A. PHP is Linux
         B. is Linux
         C. PHP is inux
         D. PHP is
 
 
       2.如下代碼運行的結果爲?
 
         $first = "This course is very easy!" ;
         $second = explode ( " " , $first );
         $first = implode( "," , $second );
         echo $first ;
 
         A. This,course,is,very,easy!
         B. This course is very easy!
         C. This course is very easy!,
         D. 提示錯誤
 
 
       3.下列哪一個函數是將字符串先後顛倒?
 
         A. strrev ();
         B. strpos ();
         C. strstr ();
         D. ucfirst();
 
       4.如下程序程序運行結果爲: (選擇題)
         Array([0]=>@test [1]=>com [2]=>cn)
         橫線處應該使用的函數爲?
         $email = "abc@test.com.cn" ;
         $str = _______( $email , ‘@’);
         $info = _______(‘.’, $str );
         ______( $info );
 
         A. strchr , split, var_dump
         B. strstr , explode , print_r
         C. strstr , explode , echo
         D. strchr , split, var_dump
 
       5. 在 str_replace (1, 2, 3)函數中1 2 3 正確的順序爲:
         A. 取代字符串, 被取代字符串, 來源字符串
         B. 被取代字符串, 取代字符串, 來源字符串
         C. 來源字符串, 取代字符串, 被取代字符串
         D. 來源字符串, 被取代字符串, 取代字符串
相關文章
相關標籤/搜索