Explode() vs Preg_split

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
explode() vs PREG_SPLIT​

G'day everyone.

Welcome to my tutorial. This one I will be showing you the difference between PREG_SPLIT and explode(). Thanks to Jordan for showing me the explode() function before I got stuck into using PREG_SPLIT.

I believe that they each have different uses as PREG_SPLIT. If you don't need the power of regular expressions use explode() if not use PREG_SPLIT. One of the main differences is that PREG_SPLIT has flags that help customise the function as explode() doesn't. Let's check out the syntax for them.
PREG_SPLIT


explode()


As you can see they are very similar.

Let's see them in action shall we? OK let's say I wanted the user to input some tags to be stored in a SQL database so when searched is searches the tags. Each tag should then be seperated by a comma. Let's check this out shall we;
Code:
$tags = "Brandon W, tutorial, function, php, explode";

We have set a variable that will retrieve the tags, in this example which just stored some tags in a variable. But let's see PREG_SPLIT in action firstly;
Code:
$split = preg_split('/, /', $tags, -1, PREG_SPLIT_NO_EMPTY);

How does the PREG_SPLIT function work? First of all the first paramaters set on which string should be reconginsed and be split into an array called $split. They are written in between "/" tags. The second parameters sets the string that you would like preg_split to reconigse. The third parameters sets the length of the string you would like to preg_split to split to. In this case it is -1, this means that there is no length it just searches the entire string. The last set of parameters is a flag, there are three built in flags;


As you can see, you can set multiple flags by using the "|" operator.


Now for a more simplier syntax, the explode() function. Let's re-write the above code but time using the explode function.
Code:
print_r(explode(", ", $tags));

As you can see this one is more more simple. The first set of paramaters sets which string you would like explode to look for. The second paramaters then sets which string explode has to look for and last but not least. The final set of paramaters then sets the length of the string to search in.

The opposite of the explode() function is the implode function which uses the same syntax. To test if this had worked correctly, add the following code below.
Code:
echo "<pre>";
echo $tags;
echo "</pre>";

This should now echo the answer and you will see if you are correct or not. In my opinion the explode() function is more cleaner and simplier to use. Here is the complete code for the explode() function.

Code:
$tags = "Brandon W, tutorial, function, php, explode";
print_r(explode(", ", $tags));

This should show the array correctly, if not add the pre tags.

Code:
$tags = "Brandon W, tutorial, function, php, explode";
echo "<pre>";
print_r(explode(", ", $tags));
echo "</pre>";


Hope you enjoyed this tutorial as much as I wrote it. I hope I gave you a better understanding between the two and will know which one for one which situation.

EDIT:

NOTE by Jordan




Take care and enjoy,
Brandon

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

Top