explode


string apg

SPLITS a given STRING into several others.





This function returns an ARRAY of STRINGS, each of which is a SUBSTRING of STRING formed by splitting it on boundaries formed by the STRING delimiter.



<?php

arr explode 
str $delimiter 
                     
str $string [, 
                     
int $limit PHP_INT_MAX ] )



where,

$delimiter The boundary STRING used as 
                                                
a separator of terms

$string 
The input STRING

$limit 
The number of elements of the STRING
             
SEE the below TABLE )

?>

$delimiter


If $delimiter = "", (empty STRING), this function 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.


$string


The input STRING.



$limit

CONSTANT VALUES OBSERVATION
PHP_INT_MIN -9223372036854775808
Since PHP 5.1.0
If the limit parameter is negative,
all components except the last -limit are returned.
  0 If the limit parameter is zero,
then this is treated as 1.
PHP_INT_MAX 9223372036854775807 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.
ed48


  1 EXERCISE   

<?php

$xyz 
"STRING DELIMITED BY DOUBLE QUOTATION MARKS";  

$arrxyz explode(" "$xyz);

echo 
'<pre>';
print_r($arrxyz);
echo 
'<pre>';

?>

 RESULT   

Array
(
[0] => STRING
[1] => DELIMITED
[2] => BY
[3] => DOUBLE
[4] => QUOTATION
[5] => MARKS
)



  2 EXERCISE   

<?php

$dat02 

"AEAD, Autenticated Encryption with Associated Data,
is a form of encryption which simultaneously provides 
confidentiality, integrity, and authenticity assurances 
on the data.<br><br>GCM, Galois/Counter Mode is a mode 
of operation for symmetric key cryptographic block ciphers 
that has been widely adopted because of its efficiency 
and performance.<br><br>GCM throughput rates for 
state-of-the-art, high-speed communication channels 
can be achieved with reasonable hardware resources.<br>
CCM, (Counter with CBC-MAC) is a mode of operation 
for cryptographic block ciphers.<br>
It is an authenticated encryption algorithm designed 
to provide both authentication and confidentiality.
<br><br>CCM mode is only defined for block ciphers 
with a block length of 128 bits.<br><br>"
;  

$arrdat02 explode("<br>"$dat02);

print_r($arrdat02);

?>

 RESULT   

Array ( [0] => AEAD, Autenticated Encryption with Associated Data, is a form of encryption which simultaneously provides confidentiality, integrity, and authenticity assurances on the data. [1] => [2] => GCM, Galois/Counter Mode is a mode of operation for symmetric key cryptographic block ciphers that has been widely adopted because of its efficiency and performance. [3] => [4] => GCM throughput rates for state-of-the-art, high-speed communication channels can be achieved with reasonable hardware resources. [5] => CCM, (Counter with CBC-MAC) is a mode of operation for cryptographic block ciphers. [6] => It is an authenticated encryption algorithm designed to provide both authentication and confidentiality. [7] => [8] => CCM mode is only defined for block ciphers with a block length of 128 bits. [9] => [10] => )


  3 EXERCISE   

<?php

$dat03 

"AEAD, Autenticated Encryption with Associated Data, 
is a form of encryption which simultaneously provides 
confidentiality, integrity, and authenticity assurances 
on the data.<br><br>GCM, Galois/Counter Mode is a mode 
of operation for symmetric key cryptographic block ciphers 
that has been widely adopted because of its efficiency 
and performance.<br><br>GCM throughput rates for state-of-the-art, 
high-speed communication channels can be achieved with 
reasonable hardware resources.<br>CCM, (Counter with CBC-MAC) 
is a mode of operation for cryptographic block ciphers.<br><br>
It is an authenticated encryption algorithm designed 
to provide both authentication and confidentiality.<br><br>
CCM mode is only defined for block ciphers with a block 
length of 128 bits.<br><br>"
;  

$arrdat03 explode("<br><br>"$dat03);

$ct03 count($arrdat03);

for(
$x03 0$x03 $ct03$x03++)
{

echo 
$arrdat03[$x03] . '<br><br>';

}

?>

 RESULT   

AEAD, Autenticated Encryption with Associated Data, is a form of encryption which simultaneously provides confidentiality, integrity, and authenticity assurances on the data.

GCM, Galois/Counter Mode is a mode of operation for symmetric key cryptographic block ciphers that has been widely adopted because of its efficiency and performance.

GCM throughput rates for state-of-the-art, high-speed communication channels can be achieved with reasonable hardware resources.
CCM, (Counter with CBC-MAC) is a mode of operation for cryptographic block ciphers.

It is an authenticated encryption algorithm designed to provide both authentication and confidentiality.

CCM mode is only defined for block ciphers with a block length of 128 bits.


  4 EXERCISE   

<?php

echo "Testing explode() function: misc tests.<br>";

$str "one\x00two\x00three\x00four";

echo 
"<br><br>1. positive limit with null separator:<br>";
$e test_explode("\x00"$str2);

echo 
"<br><br> negative limit (since PHP 5.1)
                        with null separator:<br>"
;
$e test_explode("\x00"$str, -2);

echo 
"<br><br>2. unknown string:<br>";
$e test_explode("fred"$str,1);

echo 
"<br><br>3. limit = 0:<br>";
$e test_explode("\x00"$str0);

echo 
"<br><br>4. limit = -1:<br>";
$e test_explode("\x00"$str, -1);

echo 
"<br><br>5. large limit = -100:<br>";
$e test_explode("\x00"$str100);

function 
test_explode($delim$string$limit)
{
    
$e explode($delim$string$limit);
    foreach ( 
$e as $v)
    {
        
var_dump(($v));
    }
}
?>

  5 EXERCISE   

<?php

echo "Testing explode() function: 
            positive and negative limits.<br>"
;

$str 'one||two||three||four';

echo 
"<br><br>1. positive limit:<br>";
var_dump(explode('||'$str2));

echo 
"<br><br>2. negative limit (since PHP 5.1):<br>";
var_dump(explode('||'$str, -1));

echo 
"<br><br>3. negative limit (since PHP 5.1) 
                   with null string:<br>"
;
var_dump(explode('||'"", -1));

?>

  6 EXERCISE   

<?php

echo "Testing explode() function: match longer string.<br><br>";

$pizza  "piece1 piece2 piece3 piece4 piece5 piece6 p";

$pieces explode(" p"$pizza);

var_dump($pieces);

?>

  7 EXERCISE   

<?php

$res 
explode("A",145999999);

var_dump($res);

?>

  8 EXERCISE   

<?php

echo "Testing explode() for basic operations.<br><br>";

$delimiters = array (
  
"",  // len=0
  
NULL,
  
"abcd",  // string
  
0,  // zero
  
"0",
  
TRUE,  // boolean value
  
FALSE,
  -
1,  // negative integer
  
-11.23,  // double
  
4,  // positive integer
  
"%",
);
$string "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND";
/* loop prints an array of strings, 
 * each of which is a substring of $string
 * formed by splitting it on boundaries formed 
 * by the string $delimiter.
 */
$counter 1;
foreach(
$delimiters as $delimiter) {
    echo 
"<br><br>Iteration: $counter<br>";

    try {
        
var_dumpexplode($delimiter$string, -1) );
    } catch (
\ValueError $e) {
        echo 
$e->getMessage() . "<br>";
    }
    try {
        
var_dumpexplode($delimiter$string0) );
    } catch (
\ValueError $e) {
        echo 
$e->getMessage() . "<br>";
    }
    try {
        
var_dumpexplode($delimiter$string1) );
    } catch (
\ValueError $e) {
        echo 
$e->getMessage() . "<br>";
    }
    try {
        
var_dumpexplode($delimiter$string2) );
    } catch (
\ValueError $e) {
        echo 
$e->getMessage() . "<br>";
    }
    
$counter++;
}

echo 
"<br><br>Testing explode() 
            with miscellaneous input arguments.<br>"
;

echo 
"<br><br>Passing positive values of 
                   Limit to explode()<br>"
;
/* LIMIT=2 */
var_dump
         
explode("::""mon::tues::wed::thurs::fri::sat::sun"2)
         );

/* checking for LIMIT =0,1 */
echo "<br><br>Passing limit values 0 and 1 to explode()<br>";
var_dumpexplode(":""Name:Phone:Address:City:State"0) );
var_dumpexplode(":""Name:Phone:Address:City:State"1) );

/* to check the maximum limit of 
 * string that can be given 
 * with limit<=0, default size is 
 * 50 but increases dynamically */
echo "<br><br>Testing explode() 
               for maximum limit of 
                        string with Limit = -1.<br>"
;
var_dumpexplode(":""1:2:3:4:5:6:7:7:5:
6:7:3:4:5:2:8:9:0:5:5:5:5:5:5:5:
5:5:5:5:5:55:5:5:5%:%:%:%:5:
5:5:%:%:5:5:5:5:5%:%:%:55:1:1"
, -1) );

echo 
"<br><br>Testing explode() with 
           string variations as input argument.<br>"
;
/* String with escape characters */
echo "<br><br>Testing string with escape characters<br>";
var_dumpexplode("\t\n""1234\t\n5678\n\t9100") );
var_dumpexplode("\r""1234\rabcd\r5678\rrstu") );

/* String with embedded NULL */
echo "<br><br>Testing string with embedded NULL<br>";
var_dumpexplode("\x00""abcd\x0n1234\x0005678
\x0000efgh\xijkl"
) );
var_dumpexplode("\0""abcd\0efgh\0ijkl\x00mnop\
x000qrst\00uvwx\000yz"
) );

/* Checking OBJECTS type */
echo "<br><br>Testing explode() with objects.<br>";
class 
string1 {
  public function 
__toString() {
    return 
"Object";
  }
}
$obj = new string1;
var_dumpexplode("b"$obj) );

?>