Tuesday, October 27, 2015

How do you use the PHP Explode function?

This post describes some examples run on a Windows 10's Internet Information Services. This is relevant for PHP 4 and PHP 5.

First directly from documentation:
array explode ( string $delimiter , string $string [, int $limit ] )

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.

Parameters ¶


The boundary string.


The input string.


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.

If the limit parameter is negative, all components except the last -limit are returned.

If the limit parameter is zero, then this is treated as 1.

Here is an example PHP file (a text file saved with the extension php) in the root directory (wwwroot) of Inetpub on a computer running Windows 10.

Explode Function

$str="A thing of beauty, is a joy for ever!";
print_r(explode(" ",$str));

At first the file should not be saved to wwwroot with extension txt, it must be php

In the SaveAs window it can he saved as ANSI, Unicode, UTF-8 or Unicode Big Endian - you have to choose the correct one.

In the first position of the explode() function you need to choose the delimiter which can be, space, comma, etc and then the $str variable is to be set.

Here are some examples with parsed content. If there is no response as in some examples here, a FALSE is returned. Also if a delimiter is not present, the whole string is returned.

Parsing a string in Japanese

No comments: