php基礎知識(字符串函數)

輸出函數:

echo(); 

print(); 

print_r();

printf();  

sprintf(); 

dump();

die();  

exit();

去掉字符:

ltrim($string);  

rtrim($string);   

trim($string,$arg1);

(參數:第二個參數指定要刪除的字符)

 

ltrim($str1,'1');    

ltrim($str1,'0..9');

添加字符串:

str_pad();

添加字符串(默認在右邊添加)

 

str_pad($str1,10,"@");               // hello@@@@@

str_pad($str,10,"@",STR_PAD_BOTH);   // @@hello@@@

str_pad($str,10,"@",STR_PAD_LEFT)    // @@@@@hello

大小寫轉換:

strtoupper();

所有字符都轉化成大寫

strtolower();  

所有字符都轉化成小寫

ucfirst();   

首字母轉化成大寫

ucword();  

每個單詞首字母轉成大寫

 

strtolower($str);  // my name is tom!

strtoupper($str); // MY NAME IS TOM!

ucfirst($str);     // My name is tom!

ucwords($str);     // My Name Is Tom!

 

Substr模式

substr( 字符串, 字符串開始位置, 返回字符串長度)

 

substr("Hello world",6);  // world

 

substr_compare( 字符串1, 字符串2, 從字符串1的何處比較)

 

substr_compare("Hello world","Hello world",0);  //0

 

substr_count(規定要檢查的字符串, 規定要檢索的字符串, 何處開始搜索, 搜索的長度)

計算規定檢索字符串出現的次數

 

substr_count( "Hello world. The world is nice" , "world" );  //2

 

substr_replace(字符串, 插入值, 何處替換, 替換多少個)

函數把字符串的一部分替換爲另一個字符串

 

substr_replace("Hello world","earth",6);  //Hello earth

 

Str模式

str_replace(查找值, 替換值, 搜索的字符串)

替換字符串中的字符

 

str_replace("world","Peter","Hello world!");  //Hello Peter!

 

str_shuffle(規定打亂的字符串);

隨機打亂字符串中的字符

 

str_shuffle("Hello World");    //lolWo drHle

 

str_split(規定分割的數組);

 

str_split("Hello"); // Array ( [0] => H [1] => e [2] => l [3] => l [4] => o )

 

strlen(規定的數組)

 

strlen("Hello");   //5

 

strtotime()

strrev(字符串)    

函數反轉字符串

 

strrev("Hello World!");

//!dlroW olleH

 

 

Chr && ord

chr(ASCII值)

返回的是字符串

 

chr(046);      //&

 

ord(字符串)

返回的是數字

 

ord("hello");     //104

 

分割 && 聚合

Implode(規定格式,字符串的數組)

數組元素組合成的字符串

 

$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);

//Hello World! Beautiful Day!

 

Explode(在哪分割字符串,字符串,返回數目)

 

$str = "www.runoob.com";

print_r (explode(".",$str));

Array
(
    [0] => www
    [1] => runoob
    [2] => com
)

 

富文本框

htmlentities(字符串);      

函數把字符轉換成html實體

 

$str = "<© W3CSçh°°¦§>";
echo htmlentities($str);

//<© W3CSçh°°¦§>

 

htmlspciacharts();   

函數把一些預定義的字符轉換爲html實體

 

$str = "This is some <b>bold</b> text.";

echo htmlspecialchars($str);

//This is some <b>bold</b> text.

 

strip_tags();       

函數剝去HTML xml php的標籤

 

其他

number_format(需要格式化的數據,規定多少個小數(如果不填這個,默認是千分位格式化數字));     

 

number_format("1000000");     // 1,000,000

number_format("1000000",2);   //1,000,000.00

 

chunk_split(分割的字符串,符串塊的長度,字符串後放置的內容)

函數把字符串分割爲一連串更小的部分。

 

$str = "Hello world!";
echo chunk_split($str,6,"...");

//Hello ...world!...

 

parse_str(解析的字符串,規定存儲變量的數組名稱)

parse_str("name=Peter&age=43",$myArray);
print_r($myArray);

//Array ( [name] => Peter [age] => 43 )

發佈了57 篇原創文章 · 獲贊 1 · 訪問量 3741
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章