+ Reply to Thread
Results 1 to 4 of 4

Thread: Split string

  1. #1
    radofeya is offline x10Hosting Member radofeya is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    49

    Split string

    Can I split a string word by word into an array by using PHP?

    example:

    $string = "hello";
    then, split string into "h", "e", "l", "l", "o".

    thanks

  2. #2
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: Split string

    There's a function just for that
    PHP Code:
    $string 'Hello there!';
    $array str_split($string);
    print_r($array); 
    The above will output:
    Code:
    Array
    (
        [0] => H
        [1] => e
        [2] => l
        [3] => l
        [4] => o
        [5] =>
        [6] => t
        [7] => h
        [8] => e
        [9] => r
        [10] => e
        [11] => !
    )
    So the function you need is str_split. You can also choose to split the string every 2 letters, 3 letters, or whatever you desire, but the default will split every letter into it's own array key.
    View PHP.net for more info.
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  3. #3
    scopey is offline x10Hosting Member scopey is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    62

    Re: Split string

    Similarly, you can just use explode() if you split by null:

    PHP Code:
    // returns an array with each character:
    explode("Hello there!"""); 
    - When in doubt, refer to the PHP manual.

  4. #4
    radofeya is offline x10Hosting Member radofeya is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    49

    Re: Split string

    Waw, that is great. Thanks.

+ Reply to Thread

Similar Threads

  1. PHP Word Scrambler - String Questions
    By masterjake in forum Programming Help
    Replies: 2
    Last Post: 09-17-2008, 03:51 PM
  2. Multibyte String Functions
    By sprocket in forum Free Hosting
    Replies: 1
    Last Post: 08-13-2008, 04:36 AM
  3. split account?
    By Matt in forum Free Hosting
    Replies: 11
    Last Post: 06-29-2005, 01:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers