Results 1 to 7 of 7

Thread: Splitting string into array with delimiters

  1. #1
    Join Date
    May 2011
    Posts
    30

    Splitting string into array with delimiters

    Hello my programmers friends, how are you all? Hope all are doing well and are fine too. I am again here with some sort of head ace. I mean I have a small doubt for which I am seeking your help. So please me. The thing is that I want to chop the text using two or more delimiters and store it in the array format. I have completely no idea what to do as I am new in the programming field. So please help me put and thank you to all in advance

  2. #2
    Join Date
    May 2009
    Posts
    527

    Re: Splitting string into array with delimiters

    You can use a preg_split() function, this will help you in splitting up the string. It splits up the string using a regular expression match which is for the delimiters. The explode() function can also be used for splitting up the string in to substrings but it can use only the simple string as the delimiter. But I would say that explode() is fast enough than regular expression, but you should still around to the preg_split() for the better understanding.

  3. #3
    Join Date
    Apr 2009
    Posts
    488

    Re: Splitting string into array with delimiters

    Yes go for preg_split.
    Syntax for it : array preg_split (string pattern, string subject [, int limit [, int flags]])
    Note: if you specify the limit, the4n returned substring will be only up to the limit but if you specify -1 then there exists no limits. You also need to set a flag for the splitting. If you set PREG_SPLIT_NO_EMPTY as a flag then the returned string would contain non-empty pieces and if you set PREG_SPLIT_DELIM_CAPTURE as a flag then parenthesized expression in the delimiter pattern will be arrested and revisited along with it.

  4. #4
    Join Date
    May 2009
    Posts
    539

    Re: Splitting string into array with delimiters

    Example: This example will split up the main string into the words.
    $string = 'Split this string as an example.';
    $words = preg_split('@[\W]+@', $string)
    print_r($words);
    Output:
    [0] => Split
    [1] => this
    [2] => string
    [3] => as
    [4] => an
    [5] => example.

  5. #5
    Join Date
    Apr 2009
    Posts
    569

    Re: Splitting string into array with delimiters

    Here you will find the example using the limits
    $str = 'Hello! Your problem is solved';
    $ans = preg_split('@[\W]+@', $str, 2);
    print_r($ans);
    Output is:
    [0] => Hello!
    [1] => Your problem is solved

  6. #6
    Join Date
    May 2009
    Posts
    529

    Re: Splitting string into array with delimiters

    I think using explode will be much easier for you as it is much simpler than the other one. Take my opinion and use explode().The specialty of explode is to break up the string into the array.
    Syntax:
    explode(separator,string,limit)
    separator: her you can specify where the string must be broken.
    String: This is the string which is to be splited.
    Limit: This will specify the number of array elements to be returned.

  7. #7
    Join Date
    May 2009
    Posts
    511

    Re: Splitting string into array with delimiters

    Here is the small example for the same i.e explode().
    <?php
    $str = "Using explode is easy.";
    print_r (explode(" ",$str));
    ?>
    Output:
    Array
    (
    [0] => Using
    [1] => explode.
    [2] => is
    [3] => easy.
    )

Similar Threads

  1. String is not occurring in array.
    By rUChIRr in forum Software Development
    Replies: 6
    Last Post: 17-06-2011, 10:20 PM
  2. How to convert string to char array in java?
    By Baazigar in forum Software Development
    Replies: 6
    Last Post: 10-01-2011, 06:36 PM
  3. JavaScript to convert an Array to String
    By KAMANA in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 05:30 AM
  4. NullPointerException when using array of string in Java
    By Sean J in forum Software Development
    Replies: 2
    Last Post: 25-04-2009, 08:14 PM
  5. PHP: array to string?
    By Diwakar_12 in forum Software Development
    Replies: 4
    Last Post: 13-04-2009, 02:44 PM

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
  •  
Page generated in 1,719,047,390.43852 seconds with 17 queries