1. mb_strlen
版本:(PHP 4 >= 4.0.6, PHP 5, PHP 7)
解釋:mb_strlen — 獲取字符串的長度
使用方式:mixed mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )
參數: str:要檢查長度的字符串。 encoding 參數:爲字符編碼。如果省略,則使用內部字符編碼。
返回值:返回具有 encoding 編碼的字符串 str 包含的字符數。 多字節的字符被計爲 1。如果給定的 encoding 無效則返回 FALSE。
mb_internal_encoding() - 設置/獲取內部字符編碼
grapheme_strlen() - Get string length in grapheme units
iconv_strlen() - 返回字符串的字符數統計
strlen() - 獲取字符串長度
2. mb_internal_encoding
版本:(PHP 4 >= 4.0.6, PHP 5, PHP 7)
解釋:mb_internal_encoding — 設置/獲取內部字符編碼
使用方式:mixed mb_internal_encoding ([ string $encoding
= mb_internal_encoding() ] )
參數: encoding
字符編碼名稱使用於 HTTP 輸入字符編碼轉換、HTTP 輸出字符編碼轉換、mbstring 模塊系列函數字符編碼轉換的默認編碼。 You should notice that the internal encoding is totally different from the one for multibyte regex.
返回值:如果設置了 encoding
,則成功時返回 TRUE
, 或者在失敗時返回 FALSE
。 In this case, the character encoding for multibyte regex is NOT changed. 如果省略了 encoding
,則返回當前的字符編碼名稱。
例子: <?php
/* 設置內部字符編碼爲 UTF-8 */
mb_internal_encoding("UTF-8");
/* 顯示當前的內部字符編碼*/
echo mb_internal_encoding();
?>
3. strlen
版本:(PHP 4, PHP 5, PHP 7)
解釋:strlen — 獲取字符串長度
使用方式:int strlen ( string $string
)
參數: string:需要計算長度的字符串。
返回值:成功則返回字符串 string
的長度;如果 string
爲空,則返回 0。
例子: <?php
$str = 'abcdef';
echo strlen($str); // 6
$str = ' ab cd ';
echo strlen($str); // 7
?>
4. addslashes
版本:(PHP 4, PHP 5, PHP 7)
解釋:addslashes — 使用反斜線引用字符串,使用 addslashes() 的例子是當你要往數據庫中輸入數據時,需要對其進行轉義。如果你使用的 DBMS 沒有一個轉義函數,並且使用 \ 來轉義特殊字符,你可以使用這個函數。 僅僅是爲了獲取插入數據庫的數據,額外的 \ 並不會插入。
使用方式:string addslashes ( string $str
)
參數: str:要轉義的字符。
返回值:返回轉義後的字符。
例子: <?php
$str = "Is your name O'reilly?"; // 輸出: Is your name O\'reilly?
echo addslashes($str);
?>
5.var_dump
版本:(PHP 4, PHP 5, PHP 7)
解釋:var_dump — 打印變量的相關信息
使用方式:void var_dump ( mixed $expression
[, mixed $...
] )
參數: expression你要打印的變量。
返回值:沒有返回值。
例子:
6.bin2hex
版本:(PHP 4, PHP 5, PHP 7)
解釋:bin2hex — 函數把包含數據的二進制字符串轉換爲十六進制值
使用方式:string bin2hex ( string $str
)
參數: str:要轉義的字符。
返回值:返回指定字符串十六進制的表示
例子:
7.chop,rtrim
版本:(PHP 4, PHP 5, PHP 7)
解釋:chop — rtrim() 的別名,rtrim — 刪除字符串末端的空白字符(或者其他字符)
使用方式:string rtrim ( string $str
[, string $character_mask
] )該函數刪除 str
末端的空白字符(或者其他字符)並返回。
參數: str:要轉義的字符。character_mask:通過指定 character_mask
,可以指定想要刪除的字符列表。簡單地列出你想要刪除的全部字符。使用 .. 格式,可以指定一個範圍。
返回值:返回改變後的字符串。
例子: <?php
$text = "\t\tThese are a few words :) ... ";
$binary = "\x09Example string\x0A";
$hello = "Hello World";
var_dump($text, $binary, $hello); print "\n";
$trimmed = rtrim($text);
var_dump($trimmed);
$trimmed = rtrim($text, " \t.");
var_dump($trimmed);
$trimmed = rtrim($hello, "Hdle");
var_dump($trimmed); // 刪除 $binary 末端的 ASCII 碼控制字符
// (包括 0 - 31)
$clean = rtrim($binary, "\x00..\x1F");
var_dump($clean);
?>
以上例子會輸出:
string(32) " These are a few words :) ... "
string(16) " Example string
"
string(11) "Hello World"
string(30) " These are a few words :) ..."
string(26) " These are a few words :)"
string(9) "Hello Wor"
string(15) " Example string"
8.chr
版本:(PHP 4, PHP 5, PHP 7)
解釋:chr — 返回指定的字符
使用方式:sstring chr ( int $ascii
)
參數: ascii Ascii 碼。
返回值:返回規定的字符。
例子: <?php
$str = "The string ends in escape: ";
$str .= chr(27); /* 在 $str 後邊增加換碼符 */
/* 通常這樣更有用 */
$str = sprintf("The string ends in escape: %c", 27);
?>
9.ord
版本:(PHP 4, PHP 5, PHP 7)
解釋:ord — 返回字符的 ASCII 碼值
使用方式:int ord ( string $string
)
參數:string一個字符。
返回值:返回整型的 ASCII 碼值。
例子: <?php
$str = "\n";
if (ord($str) == 10) {
echo "The first character of \$str is a line feed.\n";
}
?>
10.crypt
版本:(PHP 4, PHP 5, PHP 7)
解釋:crypt — 單向字符串散列
使用方式:string crypt ( string $str
[, string $salt
] ))
參數:str待散列的字符串。salt可選的鹽值字符串。如果沒有提供,算法行爲將由不同的算法實現決定,並可能導致不可預料的結束。
返回值:返回散列後的字符串或一個少於 13 字符的字符串,從而保證在失敗時與鹽值區分開來。。
例子: <?php
// 設置密碼
$password = 'mypassword';
// 獲取散列值,使用自動鹽值
$hash = crypt($password);
?>
11.htmlentities
版本:(PHP 4, PHP 5, PHP 7)
解釋:htmlentities — 將字符轉換爲 HTML 轉義字符
使用方式:string htmlentities ( string $string
[, int $flags
= ENT_COMPAT | ENT_HTML401 [, string $encoding
= ini_get("default_charset") [, bool $double_encode
= true ]]] )
如果要解碼(反向操作),可以使用 html_entity_decode()。
參數:string輸入字符。flags以下一組位掩碼標記,用於設置如何處理引號、無效代碼序列、使用文檔的類型。 默認是 ENT_COMPAT | ENT_HTML401。double_encode關閉 double_encode
時,PHP 不會轉換現有的 HTML 實體, 默認是全部轉換。
返回值:返回編碼後的字符。如果指定的編碼 encoding
裏, string
包含了無效的代碼單元序列, 沒有設置 ENT_IGNORE
或者 ENT_SUBSTITUTE
標記的情況下,會返回空字符串。
例子: <?php
$str = "A 'quote' is <b>bold</b>";
// 輸出: A 'quote' is <b>bold</b>
echo htmlentities($str);
// 輸出: A 'quote' is <b>bold</b>
echo htmlentities($str, ENT_QUOTES);
?>
12.implode
版本:(PHP 4, PHP 5, PHP 7)
解釋:implode — 將一個一維數組的值轉化爲字符串
使用方式:string implode ( string $glue
, array $pieces
)
string implode ( array $pieces
) 用 glue
將一維數組的值連接爲一個字符串。
參數:sglue默認爲空的字符串。pieces你想要轉換的數組。
返回值:返回一個字符串,其內容爲由 glue 分割開的數組的值。
例子: <?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
// Empty string when using an empty array:
var_dump(implode('hello', array())); // string(0) ""
?>
13.md5
版本:(PHP 4, PHP 5, PHP 7)
解釋:md5 — 計算字符串的 MD5 散列值
使用方式:string md5 ( string $str
[, bool $raw_output
= false ] )
參數:str原始字符串。raw_output如果可選的 raw_output
被設置爲 TRUE
,那麼 MD5 報文摘要將以16字節長度的原始二進制格式返回。
返回值:以 32 字符十六進制數字形式返回散列值。
例子: <?php
$str = 'apple';
if (md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
echo "Would you like a green or red apple?";
}
?>
14.str_replace()
版本:(PHP 4, PHP 5, PHP 7)
解釋:str_replace — 子字符串替換
使用方式:mixed str_replace ( mixed $search
, mixed $replace
, mixed $subject
[, int &$count
] ) 該函數返回一個字符串或者數組。該字符串或數組是將 subject
中全部的 search
都被 replace
替換之後的結果。
參數:如果 search
和 replace
爲數組,那麼 str_replace() 將對 subject
做二者的映射替換。如果 replace
的值的個數少於 search
的個數,多餘的替換將使用空字符串來進行。如果 search
是一個數組而 replace
是一個字符串,那麼 search
中每個元素的替換將始終使用這個字符串。該轉換不會改變大小寫。
如果 search
和 replace
都是數組,它們的值將會被依次處理。
search查找的目標值,也就是 needle。一個數組可以指定多個目標。replace是search
的替換值。一個數組可以被用來指定多重替換。subject執行替換的數組或者字符串。也就是 haystack。如果 subject
是一個數組,替換操作將遍歷整個 subject
,返回值也將是一個數組。count如果被指定,它的值將被設置爲替換髮生的次數。
返回值:該函數返回替換後的數組或者字符串。
例子:
<?php
// 賦值: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
// 賦值: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// 賦值: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
// 賦值: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;
?>
15.str_split()
版本:(PHP 5, PHP 7)
解釋:str_split — 將字符串轉換爲數組
使用方式:array str_split ( string $string
[, int $split_length
= 1 ] ) 將一個字符串轉換爲數組。
參數:string輸入字符串。split_length每一段的長度
返回值:如果指定了可選的 split_length
參數,返回數組中的每個元素均爲一個長度爲 split_length
的字符塊,否則每個字符塊爲單個字符。
如果 split_length
小於 1,返回 FALSE
。如果 split_length
參數超過了 string
超過了字符串 string
的長度,整個字符串將作爲數組僅有的一個元素返回
例子:
16.strcasecmp
版本:(PHP 4, PHP 5, PHP 7)
解釋:strcasecmp — 二進制安全比較字符串(不區分大小寫)
使用方式:int strcasecmp ( string $str1
, string $str2
)二進制安全比較字符串(不區分大小寫)。
參數:str1第一個字符串。str2第二個字符串
返回值:如果 str1
小於 str2
返回 < 0; 如果 str1
大於 str2
返回 > 0;如果兩者相等,返回 0。
例子:
17.strstr
版本:(PHP 4, PHP 5, PHP 7)
解釋:strstr — 查找字符串的首次出現
使用方式:string strstr ( string $haystack
, mixed $needle
[, bool $before_needle
= FALSE
] ) 返回 haystack
字符串從 needle
第一次出現的位置開始到 haystack
結尾的字符串。 該函數區分大小寫。如果想要不區分大小寫,請使用 stristr()。如果你僅僅想確定 needle
是否存在於 haystack
中,請使用速度更快、耗費內存更少的 strpos() 函數。
參數:haystack輸入字符串。needle如果 needle
不是一個字符串,那麼它將被轉化爲整型並且作爲字符的序號來使用。before_needle若爲 TRUE
,strstr() 將返回 needle
在 haystack
中的位置之前的部分。
返回值:返回字符串的一部分或者 FALSE
(如果未發現 needle
)。
例子:
18.substr
版本:(PHP 4, PHP 5, PHP 7)
解釋:substr — 返回字符串的子串
使用方式:string substr ( string $string
, int $start
[, int $length
] )返回字符串 string
由 start
和 length
參數指定的子字符串。
參數:string輸入字符串。必須至少有一個字符。start如果 start
是非負數,返回的字符串將從 string
的 start
位置開始,從 0 開始計算。例如,在字符串 “abcdef” 中,在位置 0 的字符是 “a”,位置 2 的字符串是 “c” 等等。
如果 start
是負數,返回的字符串將從 string
結尾處向前數第 start
個字符開始。如果 string
的長度小於 start
,將返回 FALSE
。
返回值:返回提取的子字符串, 或者在失敗時返回 FALSE
。
例子:
19.preg_match
版本:(PHP 4, PHP 5, PHP 7)
解釋:preg_match — 執行匹配正則表達式
使用方式:int preg_match ( string $pattern
, string $subject
[, array &$matches
[, int $flags
= 0 [, int $offset
= 0 ]]] )搜索subject
與pattern
給定的正則表達式的一個匹配.
參數:pattern要搜索的模式,字符串類型。subject輸入字符串。matches如果提供了參數matches
,它將被填充爲搜索結果。 $matches[0]將包含完整模式匹配到的文本, $matches[1] 將包含第一個捕獲子組匹配到的文本,以此類推。
返回值:preg_match()返回 pattern
的匹配次數。 它的值將是0次(不匹配)或1次,因爲preg_match()在第一次匹配後 將會停止搜索。preg_match_all()不同於此,它會一直搜索subject
直到到達結尾。 如果發生錯誤preg_match()返回 FALSE
。
例子:
20.settype
版本:(PHP 4, PHP 5, PHP 7)
解釋:settype — 設置變量的類型
使用方式:bool settype ( mixed &$var
, string $type
)將變量 var
的類型設置成 type
。
參數:var要轉換的變量。type
的可能值爲:boolean” (或爲“bool”,從 PHP 4.2.0 起);“integer” (或爲“int”,從 PHP 4.2.0 起);“float” (只在 PHP 4.2.0 之後可以使用,對於舊版本中使用的“double”現已停用);"string";"array";"object";“null” (從 PHP 4.2.0 起);
返回值:成功時返回 TRUE
, 或者在失敗時返回 FALSE
。
例子: