+ Reply to Thread
Results 1 to 3 of 3

Thread: Explode() vs Preg_split

  1. #1
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Explode() vs Preg_split

    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
    array preg_split ( string $pattern , string $subject [, int $limit [, int $flags ]] )
    explode()
    array explode ( string $delimiter , string $string [, int $limit ] )
    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;
    flags can be any combination of the following flags (combined with bitwise | operator):

    PREG_SPLIT_NO_EMPTY
    If this flag is set, only non-empty pieces will be returned by preg_split().
    PREG_SPLIT_DELIM_CAPTURE
    If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.
    PREG_SPLIT_OFFSET_CAPTURE
    If this flag is set, for every occurring match the appendant string offset will also be returned. Note that this changes the return value in an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
    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
    One thing to note is that preg_split() uses Perl Compatible Regular Expressions (PCRE). For a POSIX Extended Regular Expression split use the split() function.

    In any case, explode() is the fastest of the three functions if you do not need regular expressions. If you want to split by character count (rather than a patter) you may want to look into str_split().


    Take care and enjoy,
    Brandon
    Last edited by Brandon W; 10-16-2008 at 02:57 PM.
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Explode() vs Preg_split

    One thing to note is that preg_split() uses Perl Compatible Regular Expressions (PCRE). For a POSIX Extended Regular Expression split use the split() function.

    In any case, explode() is the fastest of the three functions if you do not need regular expressions. If you want to split by character count (rather than a patter) you may want to look into str_split().

    Very nice tutorial Brandon, thank you. +rep

  4. #3
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Explode() vs Preg_split

    Thanks for the information Jordan. I think I added that in but it doesn't seem like I did

    Also there is some background information which I will add to the thread tonight, thanks Jordan for the rep. I want to be registered for 30 days hehe
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts