PHP 字符串

strtoupper(string)    把字符串轉換爲大寫。該函數是二進制安全的。php

strtolower(string)    把字符串轉換爲小寫。該函數是二進制安全的。html

ucwords(string)       把字符串中每一個單詞的首字符轉換爲大寫。該函數是二進制安全的。數組

ucfirst(string)           把字符串中的首字符轉換爲大寫。該函數是二進制安全的。安全

lcfirst(string)            把字符串中的首字符轉換爲小寫。該函數是二進制安全的。函數

string 必需。規定要轉換的字符串。
echo strtoupper("Hello WORLD.");
//HELLO WORLD.

echo strtolower("Hello WORLD.");
//hello world.

echo ucwords("hello world!");
//Hello World!

echo ucfirst("hello world!");
//Hello world!

echo lcfirst("Hello world!");
//hello world!

substr(string,start,length)        返回字符串的一部分。spa

string 必需。規定要返回其中一部分的字符串。
start

必需。規定在字符串的何處開始。code

  • 正數 - 在字符串的指定位置開始
  • 負數 - 在從字符串結尾開始的指定位置開始
  • 0 - 在字符串中的第一個字符處開始
length

可選。規定被返回字符串的長度。默認是直到字符串的結尾。htm

  • 正數 - 從 start 參數所在的位置返回的長度
  • 負數 - 從字符串末端返回的長度

註釋:若是 start 參數是負數且 length 小於或等於 start,則 length 爲 0。字符串

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

echo substr("Hello world",0,-1)."<br>";
//Hello worl

substr_replace(string,replacement,start,length)    把字符串的一部分替換爲另外一個字符串。string

string 必需。規定要檢查的字符串。
replacement 必需。規定要插入的字符串。
start

必需。規定在字符串的何處開始替換。

  • 正數 - 在字符串中的指定位置開始替換
  • 負數 - 在從字符串結尾的指定位置開始替換
  • 0 - 在字符串中的第一個字符處開始替換
length

可選。規定要替換多少個字符。默認是與字符串長度相同。

  • 正數 - 被替換的字符串長度
  • 負數 - 表示待替換的子字符串結尾處距離 string 末端的字符個數。
  • 0 - 插入而非替換

註釋:若是 start 參數是負數且 length 小於或者等於 start,則 length 爲 0。

註釋:該函數是二進制安全的。

echo substr_replace("Hello","world",0);
//world

echo substr_replace("Hello world","Shanghai",6);
//Hello Shanghai

echo substr_replace("Hello world","Shanghai",-5);
//Hello Shanghai

echo substr_replace("world","Hello ",0,0);
//Hello world

$replace = array("1: AAA","2: AAA","3: AAA");
echo implode(" ",substr_replace($replace,'BBB',3,3));
//1: BBB 2: BBB 3: BBB

nl2br(string,xhtml)    

string 必需。規定要檢查的字符串。
xhtml

可選。布爾值,表示是否使用兼容 XHTML 換行:

  • TRUE- 默認。插入 <br />
  • FALSE - 插入 <br>

在字符串中的每一個新行(\n)以前插入 HTML 換行符(<br> 或 <br />)

echo nl2br("One line.\nAnother line.");
//One line.
//Another line.

explode(separator,string,limit)    把字符串打散爲數組。

separator 必需。規定在哪裏分割字符串。
string 必需。要分割的字符串。
limit

可選。規定所返回的數組元素的數目。

可能的值:

  • 大於 0 - 返回包含最多 limit 個元素的數組
  • 小於 0 - 返回包含除了最後的 -limit 個元素之外的全部元素的數組
  • 0 - 返回包含一個元素的數組

"separator" 參數不能是空字符串。該函數是二進制安全的。

 

implode()    返回由數組元素組合成的字符串。

相關文章
相關標籤/搜索