PHP explode() Function

P
PHP echo() Function
PHP fprintf() Function

In this article, you will learn how to split a string into array based on some separator. The PHP explode() function convert or breaks a string into an array where each index of the array contains the broken part of the string based on the separator.

Note: The separator cannot be an empty string. This function is binary-safe.

What is binary safe function in php?

Binary safety is a property of functions which means they process any string correctly. The converse would be a string that contains only ASCII characters and no null characters – such a string should be processed correctly by any function.

Binary safe is a special property of functions which allows them to process any string correctly. For example, if a string contains only ASCII characters and no null values, it can be processed correctly by the binary safe function. It the function is not binary safe, it may produce wrong output.

What is the syntax of the EXPLODE() function in php?

explode(separator,string,limit)
ParameterDescription
separatorSpecify the breaking point from the string – Required
stringThe string to divide into array – Required
limitIf you want to limit the number of elements in the returned array, pass the desired limits described below.
Greater than 0 – Return an array with a maximum of limit
Less than 0 – Return an array except for the last limit
Equal to 0 – Return an array containing one element
PHP EXPLODE() METHOD

Examples of the EXPLODE() function

Example 1. In this example, we break a string into an array using ” ” (space).

<?php
$str = "Hello PHP. It is the best scripting language.";
print_r (explode(" ",$str));
?>

Example 2. In this example, we use the limiter parameter with the PHP explode method to limit the number of array elements in the output.

<?php
$str = 'Sun,Mon,Tue,Wed,Thur,Fri,Sat';

// 0 limit
print_r(explode(',',$str,0));

// +ve limit
print_r(explode(',',$str,2));

// -ve limit
print_r(explode(',',$str,-1));
?>

PHP echo() Function
PHP fprintf() Function

Add comment

Tutor Network

Share this

Learn PHP from A to Z