PHP explode (): this is how you use the function to separate strings

PHP command explode ()

Many things can be done with strings using the PHP programming language. A variant of this is covered by the PHP “explode” command. As the name suggests, this "explodes" a string into several pieces, which are then placed in an array by the command.

array explode (string $ delimiter, string $ string [, int $ limit])

The $ delimiter is the character based on which the string is divided. The variable $ limit can be inserted or omitted. If you enter a value (integer) there, you only get a limited number of elements in the array.

This is how the PHP command explode works - explanation and practical examples.
This is how the PHP command explode works - explanation and practical examples.

Code example with explode ()

The PHP commands can always be explained most clearly if you program a few examples that show what comes out with “out”.

Separate at spaces

Here is an example where a sentence is split at the spaces. Of course, all examples must be in the PHP markers be bordered.

$ string = "This is an example we share a sentence with."; $ array = explode ("", $ string); echo $ array [3];

The output here will be the word “example” since the explode command uses the spaces to separate the sentence. Since an array always starts with 0 unless otherwise specified, $array[0] is the word "that" and $array[1] is "is" and $array[3] is the word "example".

Separating a URL into individual components

Splitting a URL address into its various folders is a little more practical. In the example, a URL is split and then output as an array line by line.

$ url = "https://www.php.net/manual/de/function.explode.php"; $ separator = "/"; $ array = explode ($ separator, $ url); for ($ x = 0; $ x <count ($ array); $ x ++) {echo $ array [$ x]. " ";}

The output in this example is

https: www.php.net manual de function.explode.php

Separation of a list

So that you can see that the explode function in PHP can also handle two or more characters as "delimiters" or separators, here is an example in which a list of fruits is exploded into an array, but with spaces and are separated by commas.

If you were to choose the separator only from commas without spaces, you would have to execute a trim () on the individual array elements so that they do not have an appended space.

$ url = "apple, pear, orange, grapefruit, lemon, grape"; $ separator = ","; $ array = explode ($ separator, $ url); for ($ x = 0; $ x <count ($ array); $ x ++) {echo $ array [$ x]. " ";}

 

Output:

Apple pear orange tomato lemon grape
Did you like the article and did the instructions on the blog help you? Then I would be happy if you the blog via a Steady Membership would support.

Post a comment

Your e-mail address will not be published. Required fields are marked with * marked

In the Sir Apfelot Blog you will find advice, instructions and reviews on Apple products such as the iPhone, iPad, Apple Watch, AirPods, iMac, Mac Pro, Mac Mini and Mac Studio.