- 6 years ago
- Zaid Bin Khalid
- 13,901 Views
-
8
PHP language is specially used in web development and can be embedded with HTML. Most of the web applications are developed in PHP with MySql. There are so many CMS or Framework available nowadays that are purely developed in PHP.
PHP is a proper language it has many built-in functions that we can use to do that or we can create our own function. So, technically PHP cover all the things that we need.
We are going to break the string with space for that we use explode(). It is a PHP built-in function that we use to break a string and remember it will return an array.
explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] ) : array
In above syntax explode function has 3 parameters.
- The Delimiter is a boundary string means from that boundary string will be a break.
- The second parameter is String which is the input string.
- The third parameter is Limit. You can pass an integer value init it can be positive or negative.
With positive value, the function will create that many indexes of the array and the last index have a remaining part string.
With negative value, function ignore the rest of string. If you pass nothing then function will consider to work with all string.
With positive value.
$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
$ary = explode(" ",$string, 5);
print"<pre>";
print_r($ary);
print"</pre>";
The out put will be.
Array
(
[0] => Lorem
[1] => Ipsum
[2] => is
[3] => simply
[4] => dummy text of the printing and typesetting industry.
)
With negative value.
$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
$ary = explode(" ",$string, -5);
print"<pre>";
print_r($ary);
print"</pre>";
The output will be.
Array
(
[0] => Lorem
[1] => Ipsum
[2] => is
[3] => simply
[4] => dummy
[5] => text
[6] => of
)
With no value.
$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
$ary = explode(" ",$string);
print"<pre>";
print_r($ary);
print"</pre>";
The output will be.
Array
(
[0] => Lorem
[1] => Ipsum
[2] => is
[3] => simply
[4] => dummy
[5] => text
[6] => of
[7] => the
[8] => printing
[9] => and
[10] => typesetting
[11] => industry.
)
Hance: explode is a main function that you can use to break any string with any character in above example of code we use space you can use any special character.
- 6 years ago
- Zaid Bin Khalid
- 13,901 Views
-
8