The explode () function of the PHP string is used

Source: Internet
Author: User

Explode
  • (PHP 4, PHP 5, PHP 7)
  • Explode-split a string by string
  • explode-using one string to split another string
Description
arrayexplode(     $delimiter,     $string[,     $limitPHP_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 作为边界点分割出来。
Parametersdelimiter
    • The boundary string.
    • The delimited character on the boundary.
String
    • The input string.
    • The input string.
Limit
    • If limit is set and positive, the returned array would contain a maximum of limit elements with the last element containing The rest of the string.
    • If the limit parameter is set and is a positive number, the returned array contains a maximum of limit elements, and the last element will contain the remainder of the string.

    • If the limit parameter is negative, all components except the Last-limit are returned.
    • If the limit parameter is a negative number, all elements except the last-limit element are returned.

    • If the limit parameter is zero and then this is treated as 1.
    • If limit is 0, it will be treated as 1.

Note:

  • Although implode () can, for historical reasons, accept it parameters in either order, explode () cannot. You must ensure that the delimiter argument comes before the string argument.
  • For historical reasons, although implode () can receive two parameter sequences, explode () does not work. You must ensure that the separator parameter is not preceded by a string parameter.
Return Values
    • Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.
    • This function returns an array of strings, each of which is a substring of string, separated by the delimiter as a boundary point.

    • If delimiter is an empty string (""), explode () would return FALSE. If delimiter contains a value that's not contained in string and a negative limit are used, then an empty array would be re Turned, otherwise an array containing string would be returned.
    • If delimiter is an empty string (""), Explode () returns FALSE. If the value contained in the delimiter is not found in the string and a negative limit is used, then an empty array is returned, otherwise an array containing a single element of string is returned.

Example
<?php/*** Created by Phpstorm.* User:zhangrongxiang* DATE:2018/2/16* Time: PM 3:49 *////Example 1$pizza="Piece1 piece2 piece3 piece4 piece5 piece6";$pieces=Explode( " ", $pizza );Echo $pieces[0].Php_eol; //Piece1Echo $pieces[1].Php_eol; //Piece2///Example 2$data="Foo:*:1023:1000::/home/foo:/bin/sh";List( $user, $pass, $uid, $gid, $gecos, $home, $shell )=Explode( ":", $data );Echo $user.Php_eol; //FooEcho $pass.Php_eol; // *Print_r( Explode( ';', $data ) );//[0] = = foo:*:1023:1000::/home/foo:/bin/sh$str=' One|two|three|four ';//Positive limit//[0] = One//[1] = Two|three|fourPrint_r( Explode( '|', $str, 2 ) );//Negative limit (since PHP 5.1)//[0] = One//[1] =//[2] = three//If the limit parameter is a negative number, all elements except the last-limit element are returned. 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 );/////////////////////////////////////////////////////////////////////////////////////functionMultiexplode( $delimiters, $string ){$ready=Str_replace( $delimiters, $delimiters[0], $string );    //here is a sample, this text, and this would 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'll be exploded//[3] = This also//[4] = this one too//[5] =)$text="Here's a sample:this text, and this would 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 elementsArray_map( ' Trim ', Explode( ",", $str ) );$str="One, the other, three, four";//[0] = One//[1] =//[2] = three//[3] + FourPrint_r( Array_map( ' Trim ', Explode( ",", $str ) ) );///////////////////////////////////////////////////////////////////////////////////////the function//param 1 have to is an Array//param 2 have to is a StringfunctionMultiexplode2( $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/function.explode.php
All rights reserved

The explode () function of the PHP string is used

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.