php常用字符串函數大全


 

1.strstr(string $str,mixed $needle[, bool $before_needle = FALSE ])
參數 $str  輸入的字符串   ,$needle  查找的字符串,$before_needle 布爾值
$str = 'abc@yui'
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

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM