摘要:由于歷史原因,雖然可以接收兩種參數(shù)順序,但是不行。此函數(shù)返回由字符串組成的,每個元素都是的一個子串,它們被字符串作為邊界點(diǎn)分割出來。如果所包含的值在中找不到,并且使用了負(fù)數(shù)的,那么會返回空的,否則返回包含單個元素的數(shù)組。
explode
Description(PHP 4, PHP 5, PHP 7)
explode — Split a string by string
explode — 使用一個字符串分割另一個字符串
array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] ) //Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter. //此函數(shù)返回由字符串組成的數(shù)組,每個元素都是 string 的一個子串,它們被字符串 delimiter 作為邊界點(diǎn)分割出來。Parameters delimiter
The boundary string.
邊界上的分隔字符。
stringThe input string.
輸入的字符串。
limitIf limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
如果設(shè)置了 limit 參數(shù)并且是正數(shù),則返回的數(shù)組包含最多 limit 個元素,而最后那個元素將包含 string 的剩余部分。
If the limit parameter is negative, all components except the last -limit are returned.
如果 limit 參數(shù)是負(fù)數(shù),則返回除了最后的 -limit 個元素外的所有元素。
If the limit parameter is zero, then this is treated as 1.
如果 limit 是 0,則會被當(dāng)做 1。
Note:
Return ValuesAlthough implode() can, for historical reasons, accept its parameters in either order, explode() cannot. You must ensure that the delimiter argument comes before the string argument.
由于歷史原因,雖然 implode() 可以接收兩種參數(shù)順序,但是 explode() 不行。你必須保證 separator 參數(shù)在 string 參數(shù)之前才行。
Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.
此函數(shù)返回由字符串組成的 array,每個元素都是 string 的一個子串,它們被字符串 delimiter 作為邊界點(diǎn)分割出來。
If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.
如果 delimiter 為空字符串(""),explode() 將返回 FALSE。 如果 delimiter 所包含的值在 string 中找不到,并且使用了負(fù)數(shù)的 limit , 那么會返回空的 array, 否則返回包含 string 單個元素的數(shù)組。
Examplefoo:*:1023:1000::/home/foo:/bin/sh $str = "one|two|three|four"; // 正數(shù)的 limit //[0] => one //[1] => two|three|four print_r( explode( "|", $str, 2 ) ); // 負(fù)數(shù)的 limit(自 PHP 5.1 起) //[0] => one //[1] => two //[2] => three //如果 limit 參數(shù)是負(fù)數(shù),則返回除了最后的 -limit 個元素外的所有元素。 print_r( explode( "|", $str, - 1 ) ); $path = "/Users/zhangrongxiang/WorkSpace/phpProjects/PHPTEST"; //[0] => //[1] => Users //[2] => zhangrongxiang //[3] => WorkSpace //[4] => phpProjects //[5] => PHPTEST $rs = explode( "/", $path ); print_r( $rs ); //[0] => //[1] => Users //[2] => zhangrongxiang/WorkSpace/phpProjects/PHPTEST $rs = explode( "/", $path, 3 ); print_r( $rs ); //[0] => //[1] => Users //[2] => zhangrongxiang $rs = explode( "/", $path, - 3 ); print_r( $rs ); ///////////////////////////////////////////////////////////////////////////////////// function multiexplode( $delimiters, $string ) { $ready = str_replace( $delimiters, $delimiters[0], $string ); //here is a sample, this text, and this will be exploded, this also , this one too ,) echo $ready . PHP_EOL; $launch = explode( $delimiters[0], $ready ); return $launch; } //[0] => here is a sample //[1] => this text //[2] => and this will be exploded //[3] => this also //[4] => this one too //[5] => ) $text = "here is a sample: this text, and this will be exploded. this also | this one too :)"; $exploded = multiexplode( array( ",", ".", "|", ":" ), $text ); print_r( $exploded ); ///////////////////////////////////////////////////////////////////////////////////// $str = ""; $res = explode( ",", $str ); //Array //( // [0] => //) print_r( $res ); $res = array_filter( explode( ",", $str ) ); //Array //( //) print_r( $res ); ///////////////////////////////////////////////////////////////////////////////////// //a simple one line method to explode & trim whitespaces from the exploded elements array_map( "trim", explode( ",", $str ) ); $str = "one ,two , three , four "; //[0] => one //[1] => two //[2] => three //[3] => four print_r( array_map( "trim", explode( ",", $str ) ) ); ///////////////////////////////////////////////////////////////////////////////////// //the function //Param 1 has to be an Array //Param 2 has to be a String function multiexplode2( $delimiters, $string ) { $ary = explode( $delimiters[0], $string ); array_shift( $delimiters ); if ( $delimiters != null ) { foreach ( $ary as $key => $val ) { $ary[ $key ] = multiexplode2( $delimiters, $val ); } } return $ary; } // Example of use $string = "1-2-3|4-5|6:7-8-9-0|1,2:3-4|5"; $delimiters = Array( ",", ":", "|", "-" ); $res = multiexplode2( $delimiters, $string ); print_r( $res );See
http://php.net/manual/en/func...
All rights reserved文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/28335.html
摘要:將一個一維數(shù)組的值轉(zhuǎn)化為字符串用將一維數(shù)組的值連接為一個字符串。因為歷史原因,可以接收兩種參數(shù)順序,但是不行。不過按文檔中的順序可以避免混淆。默認(rèn)為空的字符串。你想要轉(zhuǎn)換的數(shù)組。返回一個字符串,其內(nèi)容為由分割開的數(shù)組的值。 implode (PHP 4, PHP 5, PHP 7) implode — Join array elements with a string implode...
摘要:規(guī)定要檢查的字符串。遇到這種情況時可以使用函數(shù)進(jìn)行檢測。輸出反引用一個引用字符串函數(shù)示例反引用一個引用字符串輸出連接分割字符串使用一個字符串分割另一個字符串邊界上的分隔字符。應(yīng)使用運(yùn)算符來測試返回值函數(shù)示例輸出返回字符串的子串輸入字符串。 轉(zhuǎn)自我的github函數(shù)示例源碼 字符串的格式化 rtrim(),除字符串右端的空白字符或其他預(yù)定義字符 ltrim(),刪除字符串開頭空格或...
摘要:超過三個參數(shù)會導(dǎo)致參數(shù)之間的組合過多,你必須對每個單獨(dú)的參數(shù)測試大量不同的情況。拆分這些函數(shù),可以讓代碼可重用性更高且更易測試。 函數(shù)參數(shù)不要超過兩個 限制函數(shù)的參數(shù)數(shù)量是非常重要的,因為它使你的函數(shù)更容易測試。超過三個參數(shù)會導(dǎo)致參數(shù)之間的組合過多,你必須對每個單獨(dú)的參數(shù)測試大量不同的情況。 沒有參數(shù)是最理想的情況,一個或兩個參數(shù)是可以接受的,三個以上則是應(yīng)該避免的。這很重要的。如果你...
摘要:在實(shí)現(xiàn)里面,如果大于,則調(diào)用函數(shù)如果小于,則調(diào)用函數(shù)如果等于,則被當(dāng)做處理,此時調(diào)用函數(shù)將添加到數(shù)組中。找到分隔符的位置之后,就調(diào)用函數(shù)將分隔得到的字符串插入到返回數(shù)組里。此函數(shù)可以看作是的逆向過程。調(diào)用函數(shù)做字符串的連接。 explode和implode函數(shù)主要用作字符串和數(shù)組間轉(zhuǎn)換的操作,比如獲取一段參數(shù)后根據(jù)某個字符分割字符串,或者將一個數(shù)組的結(jié)果使用一個字符合并成一個字符串輸出...
摘要:背后性能影響還是挺大的。缺失的異常剛開始寫代碼的時候一直不明白為什么要用異常,感覺就能搞定了,為什么還要多此一舉,現(xiàn)在反而覺得的異常太少。在的時候,如果出現(xiàn)異常,可以通過來獲取。 作為一名深度 phper,我如果要黑咱們 php,就像說自己母校差一樣,大家不要見外。個人博客地址:https://mengkang.net/1368.html 故事的開始 這幾天觀察錯誤日志發(fā)現(xiàn)有一個數(shù)據(jù)...
閱讀 2038·2021-11-23 10:08
閱讀 2371·2021-11-22 15:25
閱讀 3308·2021-11-11 16:55
閱讀 806·2021-11-04 16:05
閱讀 2684·2021-09-10 10:51
閱讀 735·2019-08-29 15:38
閱讀 1614·2019-08-29 14:11
閱讀 3514·2019-08-29 12:42