资讯专栏INFORMATION COLUMN

PHP之string之explode()函数使用

wenzi / 1170人阅读

摘要:由于历史原因,虽然可以接收两种参数顺序,但是不行。此函数返回由字符串组成的,每个元素都是的一个子串,它们被字符串作为边界点分割出来。如果所包含的值在中找不到,并且使用了负数的,那么会返回空的,否则返回包含单个元素的数组。

explode

(PHP 4, PHP 5, PHP 7)

explode — Split a string by string

explode — 使用一个字符串分割另一个字符串

Description
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.
//此函数返回由字符串组成的数组,每个元素都是 string 的一个子串,它们被字符串 delimiter 作为边界点分割出来。
Parameters delimiter

The boundary string.

边界上的分隔字符。

string

The input string.

输入的字符串。

limit

If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

如果设置了 limit 参数并且是正数,则返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分。

If the limit parameter is negative, all components except the last -limit are returned.

如果 limit 参数是负数,则返回除了最后的 -limit 个元素外的所有元素。

If the limit parameter is zero, then this is treated as 1.

如果 limit 是 0,则会被当做 1。

Note:

Although 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() 可以接收两种参数顺序,但是 explode() 不行。你必须保证 separator 参数在 string 参数之前才行。

Return Values

Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.

此函数返回由字符串组成的 array,每个元素都是 string 的一个子串,它们被字符串 delimiter 作为边界点分割出来。

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 中找不到,并且使用了负数的 limit , 那么会返回空的 array, 否则返回包含 string 单个元素的数组。

Example
 foo:*:1023:1000::/home/foo:/bin/sh

$str = "one|two|three|four";
// 正数的 limit
//[0] => one
//[1] => two|three|four
print_r( explode( "|", $str, 2 ) );
// 负数的 limit(自 PHP 5.1 起)
//[0] => one
//[1] => two
//[2] => three
//如果 limit 参数是负数,则返回除了最后的 -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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/28335.html

相关文章

  • PHPstringimplode()函数使用

    摘要:将一个一维数组的值转化为字符串用将一维数组的值连接为一个字符串。因为历史原因,可以接收两种参数顺序,但是不行。不过按文档中的顺序可以避免混淆。默认为空的字符串。你想要转换的数组。返回一个字符串,其内容为由分割开的数组的值。 implode (PHP 4, PHP 5, PHP 7) implode — Join array elements with a string implode...

    2json 评论0 收藏0
  • Php常用函数系列字符串处理

    摘要:规定要检查的字符串。遇到这种情况时可以使用函数进行检测。输出反引用一个引用字符串函数示例反引用一个引用字符串输出连接分割字符串使用一个字符串分割另一个字符串边界上的分隔字符。应使用运算符来测试返回值函数示例输出返回字符串的子串输入字符串。 转自我的github函数示例源码 字符串的格式化 rtrim(),除字符串右端的空白字符或其他预定义字符 ltrim(),删除字符串开头空格或...

    陆斌 评论0 收藏0
  • PHP代码简洁道——函数部分

    摘要:超过三个参数会导致参数之间的组合过多,你必须对每个单独的参数测试大量不同的情况。拆分这些函数,可以让代码可重用性更高且更易测试。 函数参数不要超过两个 限制函数的参数数量是非常重要的,因为它使你的函数更容易测试。超过三个参数会导致参数之间的组合过多,你必须对每个单独的参数测试大量不同的情况。 没有参数是最理想的情况,一个或两个参数是可以接受的,三个以上则是应该避免的。这很重要的。如果你...

    crossoverJie 评论0 收藏0
  • [PHP源码阅读]explode和implode函数

    摘要:在实现里面,如果大于,则调用函数如果小于,则调用函数如果等于,则被当做处理,此时调用函数将添加到数组中。找到分隔符的位置之后,就调用函数将分隔得到的字符串插入到返回数组里。此函数可以看作是的逆向过程。调用函数做字符串的连接。 explode和implode函数主要用作字符串和数组间转换的操作,比如获取一段参数后根据某个字符分割字符串,或者将一个数组的结果使用一个字符合并成一个字符串输出...

    Ocean 评论0 收藏0
  • PHP 批斗大会缺失的异常

    摘要:背后性能影响还是挺大的。缺失的异常刚开始写代码的时候一直不明白为什么要用异常,感觉就能搞定了,为什么还要多此一举,现在反而觉得的异常太少。在的时候,如果出现异常,可以通过来获取。 作为一名深度 phper,我如果要黑咱们 php,就像说自己母校差一样,大家不要见外。个人博客地址:https://mengkang.net/1368.html 故事的开始 这几天观察错误日志发现有一个数据...

    guqiu 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<