1. 程式人生 > >php常用字串函式大全

php常用字串函式大全

 

1.strstr(string $str,mixed $needle[, bool $before_needle = FALSE ])
引數 $str  輸入的字串   ,$needle  查詢的字串,$before_needle 布林值
$str = '[email protected]'
echo strstr($str,'@'); //@yui
//返回字串$str從@(needle)到結尾部分
echo PHP_EOL;
echo strstr($str,'@',true); //abc
//返回字串$str中的@之前的部分
2.string strrev(string $string)
返回值:反轉之後的字串
echo strrev('xp'); //px
3.strlen(string $string);
返回給定的字串 string 的位元組長度。
utf-8編碼格式的每個漢字是3個位元組
gbk編碼格式的每個漢字是2個位元組
echo strlen('漢語');//6
4.mb_strlen(string $str [, string $encoding = mb_internal_encoding() ])
返回具有 encoding 編碼的字串 str 包含的字元數。 多位元組的字元被計為 1。。
echo mb_strlen('你好','utf-8');//utf-8編碼格式的頁面,使用utf-8編碼輸出2,
echo mb_strlen('你好','gbk'); //gbk編碼格式的頁面,使用gbk編碼輸出2
5.strtolower() 返回轉換後的小寫字串。
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // 列印 mary had a little lamb and she loved it so
6.strtoupper()
* 返回轉換後的大寫字串
echo strtoupper('i love you'); // I LOVE YOU
7.ucwords(string $string)— 將字串中每個單詞的首字母轉換為大寫
echo ucwords('hello world'); // Hello World
$foo = 'hello|world you!';
echo ucwords($foo); // Hello|world You! 把字串中的單詞轉大寫
 
8.string ucfirst ( string $str )
將 str 的首字元(如果首字元是字母)轉換為大寫字母,並返回這個字串。
echo $foo = ucfirst($foo);             // Hello world!
 
9.str_replace(mixed $search,mixed $replace,string $str)
函式以其他字元替換字串中的一些字元(區分大小寫)。
該函式返回一個字串或者陣列。該字串或陣列是將 subject 中全部的 search 都被 replace 替換之後的結果。
引數
如果 search 和 replace 為陣列,那麼 str_replace() 將對 subject 做二者的對映替換。如果 replace 的值的個數少於 search 的個數,多餘的替換將使用空字串來進行。如果 search 是一個數組而 replace 是一個字串,那麼 search 中每個元素的替換將始終使用這個字串。該轉換不會改變大小寫。

如果 search 和 replace 都是陣列,它們的值將會被依次處理。

search
查詢的目標值,也就是 needle。一個數組可以指定多個目標。

replace
search 的替換值。一個數組可以被用來指定多重替換。

subject
執行替換的陣列或者字串。也就是 haystack。

如果 subject 是一個數組,替換操作將遍歷整個 subject,返回值也將是一個數組。

count
如果被指定,它的值將被設定為替換髮生的次數。

返回值
該函式返回替換後的陣列或者字串。
$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';

// 首先替換 \r\n 字元,因此它們不會被兩次轉換
echo $newstr = str_replace($order, $replace, $str);
//Line 1<br />Line 2<br />Line 3<br />Line 4<br />
// 賦值: <body text='black'>
echo $bodytag = str_replace("%body%", "black", "<body text='%body%'>");
//<body text='black'>
// 賦值: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
echo $onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// Hll Wrld f 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");

echo $newphrase = str_replace($healthy, $yummy, $phrase);
//You should eat pizza, beer, and ice cream every day
// 賦值: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count; //3

 

10. str_ireplace(mixed $search,mixed $replace,  $str);
同str_replace類似,只不過是不區分大小寫
11.將特殊字元轉換為 HTML 實體
string htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = TRUE ]]] );
$new = htmlspecialchars("<a href='test'>Test</a>");
echo $new; // &lt;a href='test'&gt;Test&lt;/a&gt;
12.
htmlspecialchars_decode(string $string) 把特殊實體轉化成字串*/
echo PHP_EOL;
echo htmlspecialchars_decode($new);  //<a href='test'>Test</a>
13.trim — 去除字串首尾處的空白字元(或者其他字元)
string trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] )
此函式返回字串 str 去除首尾空白字元後的結果。如果不指定第二個引數,trim() 將去除這些字元:
空白字元包括
" " (ASCII 32 (0x20)),普通空格符。
"\t" (ASCII 9 (0x09)),製表符。
"\n" (ASCII 10 (0x0A)),換行符。
"\r" (ASCII 13 (0x0D)),回車符。
"\0" (ASCII 0 (0x00)),空位元組符。
"\x0B" (ASCII 11 (0x0B)),垂直製表符。
$text   = "\t\tThese are a few words :) ...  ";
$binary = "\x09Example string\x0A";
$hello  = "Hello World";
var_dump($text, $binary, $hello);
/*string(32) "            These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"*/

print "\n";

$trimmed = trim($text);
var_dump($trimmed);//string(28) "These are a few words :) ..."

$trimmed = trim($text, " \t.");
var_dump($trimmed);//string(24) "These are a few words :)"

$trimmed = trim($hello, "Hdle");
var_dump($trimmed); //string(5) "o Wor"
 
14.
strpos(string $str,mixed $need[, int $offset = 0 ]) — 查詢字串首次出現的位置
引數
$str
在該字串中進行查詢。

$need
如果 needle 不是一個字串,那麼它將被轉換為整型並被視為字元的順序值。

$offset
如果提供了此引數,搜尋會從字串該字元數的起始位置開始統計。 如果是負數,搜尋會從字串結尾指定字元數開始。

返回值
返回 needle 存在於 $str 字串起始的位置(獨立於 offset)。同時注意字串位置是從0開始,而不是從1開始的。

如果沒找到 needle,將返回 FALSE。
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// 例子1 注意這裡使用的是 ===。簡單的 == 不能像我們期待的那樣工作,
// 因為 'a' 是第 0 位置上的(第一個)字元。
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
// 例子2 忽視位置偏移量之前的字元進行查詢
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0
15. stripos($str,'a’)*/;//同上 但是不區分大小寫
$foo = "0123456789a123456789b123456789c";

var_dump(strrpos($foo, '7', -4));  // 從尾部第 5 個位置開始向左查詢知道字元後輸出此字元從左向右查詢時所佔的位置
// 結果: int(17)

var_dump(strrpos($foo, '7', 20));  // 從第 20 個位置開始查詢
// 結果: int(27)

var_dump(strrpos($foo, '7', 28));  // 結果: bool(false)
/*16.
strripos($str,'a’);//同上 但是不區分大小寫*/

17
string substr ( string $string , int $start [, int $length ] )
返回字串 string 由 start 和 length 引數指定的子字串。
start (1)是非負數,從 0 開始計算.
(2)是負數,從 string 結尾處向前數第 start 個字元開始
(3)string 的長度小於 start,將返回 FALSE。
$rest = substr("abcdef", -1);    // 返回 "f"
$rest = substr("abcdef", -2);    // 返回 "ef"
$rest = substr("abcdef", -3, 1); // 返回 "d"
length
(1)正數的length,從 start 處開始最多包括 length 個字元
(2)負數的 length,那麼 string 末尾處的 length 個字元將會被省略。如果 start 不在這段文字中,那麼將返回 FALSE
(3) 0,FALSE 或 NULL 的 length,那麼將返回一個空字串。
(4)沒有提供 length,返回的子字串將從 start 位置開始直到字串結尾
$rest = substr("abcdef", 0, -1);  // 返回 "abcde"
$rest = substr("abcdef", 2, -1);  // 返回 "cde"
$rest = substr("abcdef", 4, -4);  // 返回 ""
$rest = substr("abcdef", -3, -1); // 返回 "de"
18
strrchr(string,char)
strrchr() 函式查詢字串在另一個字串中最後一次出現的位置,並返回從該位置到字串結尾的所有字元。
string 必需。規定要搜尋的字串。
char 必需。規定要查詢的字元。如果該引數是數字,則搜尋匹配此數字的 ASCII 值的字元。

echo strrchr("Hello world! What a beautiful day!",What); //搜尋 "What" 在字串中的位置,並返回從該位置到字串結尾的所有字元:
echo strrchr("Hello world!",101); //以 "o" 的 ASCII 值搜尋 "o" 在字串中的位置,並返回從該位置到字串結尾的所有字元:
19str_shuffle()隨機地打亂字串中的所有字元
echo str_shuffle('hellow'); //lehowl