摘要:由于歷史原因,雖然可以接收兩種參數(shù)順序,但是不行。此函數(shù)返回由字符串組成的,每個(gè)元素都是的一個(gè)子串,它們被字符串作為邊界點(diǎn)分割出來(lái)。如果所包含的值在中找不到,并且使用了負(fù)數(shù)的,那么會(huì)返回空的,否則返回包含單個(gè)元素的數(shù)組。
explode
Description(PHP 4, PHP 5, PHP 7)
explode — Split a string by string
explode — 使用一個(gè)字符串分割另一個(gè)字符串
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ù)組,每個(gè)元素都是 string 的一個(gè)子串,它們被字符串 delimiter 作為邊界點(diǎn)分割出來(lái)。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 個(gè)元素,而最后那個(gè)元素將包含 string 的剩余部分。
If the limit parameter is negative, all components except the last -limit are returned.
如果 limit 參數(shù)是負(fù)數(shù),則返回除了最后的 -limit 個(gè)元素外的所有元素。
If the limit parameter is zero, then this is treated as 1.
如果 limit 是 0,則會(huì)被當(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,每個(gè)元素都是 string 的一個(gè)子串,它們被字符串 delimiter 作為邊界點(diǎn)分割出來(lái)。
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 , 那么會(huì)返回空的 array, 否則返回包含 string 單個(gè)元素的數(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 個(gè)元素外的所有元素。 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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/28335.html
摘要:將一個(gè)一維數(shù)組的值轉(zhuǎn)化為字符串用將一維數(shù)組的值連接為一個(gè)字符串。因?yàn)闅v史原因,可以接收兩種參數(shù)順序,但是不行。不過(guò)按文檔中的順序可以避免混淆。默認(rèn)為空的字符串。你想要轉(zhuǎn)換的數(shù)組。返回一個(gè)字符串,其內(nèi)容為由分割開(kāi)的數(shù)組的值。 implode (PHP 4, PHP 5, PHP 7) implode — Join array elements with a string implode...
摘要:規(guī)定要檢查的字符串。遇到這種情況時(shí)可以使用函數(shù)進(jìn)行檢測(cè)。輸出反引用一個(gè)引用字符串函數(shù)示例反引用一個(gè)引用字符串輸出連接分割字符串使用一個(gè)字符串分割另一個(gè)字符串邊界上的分隔字符。應(yīng)使用運(yùn)算符來(lái)測(cè)試返回值函數(shù)示例輸出返回字符串的子串輸入字符串。 轉(zhuǎn)自我的github函數(shù)示例源碼 字符串的格式化 rtrim(),除字符串右端的空白字符或其他預(yù)定義字符 ltrim(),刪除字符串開(kāi)頭空格或...
摘要:超過(guò)三個(gè)參數(shù)會(huì)導(dǎo)致參數(shù)之間的組合過(guò)多,你必須對(duì)每個(gè)單獨(dú)的參數(shù)測(cè)試大量不同的情況。拆分這些函數(shù),可以讓代碼可重用性更高且更易測(cè)試。 函數(shù)參數(shù)不要超過(guò)兩個(gè) 限制函數(shù)的參數(shù)數(shù)量是非常重要的,因?yàn)樗鼓愕暮瘮?shù)更容易測(cè)試。超過(guò)三個(gè)參數(shù)會(huì)導(dǎo)致參數(shù)之間的組合過(guò)多,你必須對(duì)每個(gè)單獨(dú)的參數(shù)測(cè)試大量不同的情況。 沒(méi)有參數(shù)是最理想的情況,一個(gè)或兩個(gè)參數(shù)是可以接受的,三個(gè)以上則是應(yīng)該避免的。這很重要的。如果你...
摘要:在實(shí)現(xiàn)里面,如果大于,則調(diào)用函數(shù)如果小于,則調(diào)用函數(shù)如果等于,則被當(dāng)做處理,此時(shí)調(diào)用函數(shù)將添加到數(shù)組中。找到分隔符的位置之后,就調(diào)用函數(shù)將分隔得到的字符串插入到返回?cái)?shù)組里。此函數(shù)可以看作是的逆向過(guò)程。調(diào)用函數(shù)做字符串的連接。 explode和implode函數(shù)主要用作字符串和數(shù)組間轉(zhuǎn)換的操作,比如獲取一段參數(shù)后根據(jù)某個(gè)字符分割字符串,或者將一個(gè)數(shù)組的結(jié)果使用一個(gè)字符合并成一個(gè)字符串輸出...
摘要:背后性能影響還是挺大的。缺失的異常剛開(kāi)始寫(xiě)代碼的時(shí)候一直不明白為什么要用異常,感覺(jué)就能搞定了,為什么還要多此一舉,現(xiàn)在反而覺(jué)得的異常太少。在的時(shí)候,如果出現(xiàn)異常,可以通過(guò)來(lái)獲取。 作為一名深度 phper,我如果要黑咱們 php,就像說(shuō)自己母校差一樣,大家不要見(jiàn)外。個(gè)人博客地址:https://mengkang.net/1368.html 故事的開(kāi)始 這幾天觀察錯(cuò)誤日志發(fā)現(xiàn)有一個(gè)數(shù)據(jù)...
閱讀 2172·2021-11-23 10:08
閱讀 2504·2021-11-22 15:25
閱讀 3430·2021-11-11 16:55
閱讀 930·2021-11-04 16:05
閱讀 2833·2021-09-10 10:51
閱讀 848·2019-08-29 15:38
閱讀 1747·2019-08-29 14:11
閱讀 3651·2019-08-29 12:42