[28期] 字串函式總結

iteye_6481發表於2011-07-20

對於常見字串進行了大致的總結,不足之處,大家多多指教!

分割函式
explode —— 使用一個字串分割另一個字串
array explode ( string separator, string string [, int limit] )
此函式返回由字串組成的陣列,每個元素都是 string 的一個子串,它們被字串 separator 作為邊界點分割出來。如果設定了 limit 引數,則返回的陣列包含最多 limit 個元素,而最後那個元素將包含 string 的剩餘部分。
如果 separator 為空字串("",只是兩個雙引號,中間什麼也沒有),explode() 將返回 FALSE。如果 separator 所包含的值在 string 中找不到,那麼 explode() 將返回包含 string 單個元素的陣列。
如果 limit 引數是負數,則返回除了最後的 limit 個元素外的所有元素。必須保證 separator 引數在 string 引數之前才行。
<?php
// 示例1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces{0}; // piece1
echo $pieces{1}; // piece2
注:也可以用以下方式輸出,但為了區分陣列推薦用上面的
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

<?php
//示例2
$str = 'one|two|three|four';
// 正數的 limit
print_r(explode('|', $str, 2));
// 負數的 limit
print_r(explode('|', $str, -1));
?>
以上示例將輸出:
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)
連線函式:
implode —— 把陣列中的所有元素組合為一個字串,函式join()為該函式的別名
string implode ( string glue, array pieces )

此函式返回一個字串,包含陣列中所有元素且與陣列中元素順序一致,用glue連線各個元素

<?php
//示例
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

?>
注:implode中的引數可以調換位置,上例中也可這樣寫implode($array,",");

刪除字串兩端的其他字元:
trim ——刪除字串首尾的其他字元
string trim ( string str [, string charlist] )

此函式返回一個字串,這是一個去掉str首尾指定字元(charlist)的字串,如果不加第二個引數,trim()預設刪除str首尾的以下字元:
? " " (ASCII 32 (0x20)), 空格.
? "\t" (ASCII 9 (0x09)), 製表符.
? "\n" (ASCII 10 (0x0A)), 換行符.
? "\" (ASCII 13 (0x0D)), 回車符.
? "\" (ASCII 0 (0x00)), the NUL-byte.
? "\x0B" (ASCII 11 (0x0B)),垂直製表符.
<?php

$text = "\t\tThese are a few words :) ... ";

echo trim($text); // "These are a few words :) ..."
echo trim($text, " \t."); // "These are a few words :)"
?>

ltrim —— 只刪除字串首部的其他字元
string ltrim ( string str [, string charlist] )

此函式返回一個字串,這是一個去掉str首部指定字元(charlist)的字串,如果不加第二個引數,ltrim()預設刪除str首尾的字元同trim一樣

<?php
$text = "\t\tThese are a few words :) ... ";
$trimmed = ltrim($text);
// $trimmed = "These are a few words :) ... "
$trimmed = ltrim($text, " \t.");
// $trimmed = "These are a few words :) ... "
?>

rtrim —— 只刪除字串尾部的其他字元
string rtrim ( string str [, string charlist] )

此函式返回一個字串,這是一個去掉str尾部指定字元(charlist)的字串,如果不加第二個引數,ltrim()預設刪除str首尾的字元同trim一樣

<?php
$text = "\t\tThese are a few words :) ... ";
$trimmed = rtrim($text);
// $trimmed = "\t\tThese are a few words :) ..."
$trimmed = rtrim($text, " \t.");
// $trimmed = "\t\tThese are a few words :)"
?>
在以上三個函式的charlist都可以使用a..z來刪除a到z之間的所有字元,連續字元用”..”連線

替換函式:
str_replace —— 用出現在replace中的字串去替換被處理字串中的search字串
mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count] )

此函式返回一個字串或陣列,被處理字串或陣列subject中所有search字串或陣列被replace字串或陣列替換。(search屬於subject,replace是要替換為的字串)

<?php
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// Provides: 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);
// Use of the count parameter is available as of PHP 5.0.0
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count; // 2
?>
str_ireplace 替換時忽略大小寫,其用法同str_replace
注:使用陣列替換時,兩個陣列中的元素個數必須一樣

strtr —— 逐個把from字串中的每個字元替換為to字串中對應的每個字元
string strtr ( string str, string from, string to )
string strtr ( string str, array replace_pairs )

此函式返回一個字串,有兩種替換形式,一種是字串替換,一種是陣列對替換,陣列對替換中的陣列的鍵名和值分別相當於from和to,不過元素中的鍵名和值作為一個整體來逐個對應替換

<?php
$str = "http://www.phpbaiduphp.com/php/index.php";
echo $str."<br>";
$nstr=strtr($str,"comp", "net#");//$nstr =htt#://www.#h#baidu#h#.net/#h#/index.#h#
echo $nstr;
?>

<?php
$trans = array("hello" => "hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans);// hello all, I said hi
?>

重複輸出字串函式
str_repeat —— 重複輸出一個字串
string str_repeat ( string input, int multiplier )

此函式返回一個重複multiplier次的字串,如果multiplier為0,則返回空字串

<?php
echo str_repeat("-=", 10);// -=-=-=-=-=-=-=-=-=-=
?>

str_pad —— 用一個字串去填充另一個字串,最終字串長度為指定長度
string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]] )

此函式返回一個字串,填充字串在原字串的左邊,右邊或則兩邊,填充後的字串總長度為指定的長度,若指定長度小於填充後的字串,超出部分不填充。如果沒有指定填充字串pad_string,預設填充字串為空格。pad_type可以是 STR_PAD_RIGH,STR_PAD_LEFT,STR_PAD_BOTH分別代表右、左、兩邊填充方式

<?php
$input = "Alien";
echo str_pad($input, 10); // produces "Alien "
echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
echo str_pad($input, 6 , "___"); // produces "Alien_"
?>

查詢函式:
strstr —— 查詢第一次出現的的位置
string strstr ( string haystack, string needle )

此函式返回一個字串,該字串是haystack字串從第一次出現needle的位置到其字串結尾的部分字串,如果needle不存在,返回false。如果needle是整數,則把它看作ASSII編碼轉換為對應字元

<?php
$email = 'user@exam@ple.com';
$domain = strstr($email, '@');
echo $domain; // 輸出: @exam@ple.com
?>

stristr —— 意義用法同strstr,但它不區分大小寫

strops —— 查詢第一次出現的位置
int strpos ( string haystack, mixed needle [, int offset] )

此函式返回一個整數,該整數為needle第一次出現在haystack字串中的位置,如果needle不存在,則返回false。如果使用offset,則在offset前出現的needle都忽略

<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
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";
}

// We can search for the character, ignoring anything before the offset
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0
?>

strrpos —— 查詢字元最後一次出現在字串中的位置
int strrpos ( string haystack, string needle [, int offset] )

此函式返回needle在haystack中出現的最後一次的位置,如果needle不存在,返回false,offset>0時從前向後offset位置處向後查詢,offset<0時則從後往前查詢offset個位置前的最後一次出現的位置

<?php
$mystring = “acdefjbk”;
$pos = strrpos($mystring, "b");
echo $pos;//6
?>

提取函式:
substr —— 提取字串的一部分
string substr ( string string, int start [, int length] )

此函式返回從指定的開始位置start取length長度的字串,不指定長度的話,預設到字串結束,start為非負數,如果為負數,則從後往前來確定位

<?php
echo substr('abcdef', 1); // bcdef
echo substr('abcdef', 1, 3); // bcd
echo substr('abcdef', 0, 4); // abcd
echo substr('abcdef', 0, 8); // abcdef
echo substr('abcdef', -1, 1); // f
// Accessing single characters in a string
// can also be achived using "curly braces"
$string = 'abcdef';
echo $string{0}; // a
echo $string{3}; // d
echo $string{strlen($string)-1}; // f

?>
<?php
$rest = substr("abcdef", -1); // returns "f"
$rest = substr("abcdef", -2); // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
?>

相關文章