<?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 )
?>
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 |
<?php
$xyz = "STRING DELIMITED BY DOUBLE QUOTATION MARKS";
$arrxyz = explode(" ", $xyz);
echo '<pre>';
print_r($arrxyz);
echo '<pre>';
?>
<?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);
?>
<?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>';
}
?>
<?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", $str, 2);
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", $str, 0);
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", $str, 100);
function test_explode($delim, $string, $limit)
{
$e = explode($delim, $string, $limit);
foreach ( $e as $v)
{
var_dump(($v));
}
}
?>
<?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('||', $str, 2));
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));
?>
<?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);
?>
<?php
$res = explode("A",145999999);
var_dump($res);
?>
<?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_dump( explode($delimiter, $string, -1) );
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
try {
var_dump( explode($delimiter, $string, 0) );
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
try {
var_dump( explode($delimiter, $string, 1) );
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
try {
var_dump( explode($delimiter, $string, 2) );
} 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_dump( explode(":", "Name:Phone:Address:City:State", 0) );
var_dump( explode(":", "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_dump( explode(":", "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_dump( explode("\t\n", "1234\t\n5678\n\t9100") );
var_dump( explode("\r", "1234\rabcd\r5678\rrstu") );
/* String with embedded NULL */
echo "<br><br>Testing string with embedded NULL<br>";
var_dump( explode("\x00", "abcd\x0n1234\x0005678
\x0000efgh\xijkl") );
var_dump( explode("\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_dump( explode("b", $obj) );
?>