Professional Web Applications Themes

break string into an array - PHP Development

I want to break a string up into an array (eg "hello" would end up like $array[0] = h, $array[1] = e, etc) and I cannot for the life of me figure out how. things like explode() and split() both require a seperator which is more advanced than I need in this case. can anyone point me in the right direction?...

  1. #1

    Default break string into an array

    I want to break a string up into an array (eg "hello" would end up like
    $array[0] = h, $array[1] = e, etc) and I cannot for the life of me
    figure out how. things like explode() and split() both require a
    seperator which is more advanced than I need in this case. can anyone
    point me in the right direction?
    Exyle Guest

  2. #2

    Default Re: break string into an array

    I noticed that Message-ID: <42c7536b$superfeed.net> from
    Exyle contained the following:
     

    Strings are already arrays.

    To access individual letters in a string use curly braces instead of
    square. e.g.

    $string="hello"

    print $string{0}."br>";
    //prints h
    print $string{1}."br>";
    //prints e
    print $string{2}."br>";
    //prints l
    ....

    --
    Geoff Berrow 0110001001101100010000000110
    001101101011011001000110111101100111001011
    100110001101101111001011100111010101101011
    Geoff Guest

  3. #3

    Default Re: break string into an array

    Geoff Berrow wrote: 

    If this where true, the $string[0] syntax would not have been deprecated.


    JW



    Janwillem Guest

  4. #4

    Default Re: break string into an array

    Exyle wrote: 

    Besides Geoff's solution and if you really want to create an array, you can
    do the following:

    $string = 'hello';
    $arr = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);


    JW



    Janwillem Guest

  5. #5

    Default Re: break string into an array

    Thanks guys! I was looking around and managed to figure out another way
    which I'll post here incase anyone curious happens to stumble across
    this thread:

    $string = "Hello";

    for($i = 0; $i < strlen($string); $i++) {
    $array[$i] = substr($string, $i, 1);
    }
    Exyle Guest

  6. #6

    Default Re: break string into an array

    I noticed that Message-ID: <42c8734a$superfeed.net> from
    Exyle contained the following:
     

    <fx: smacks forehead> Of course, that's much easier that using
    $string{n}
    --
    Geoff Berrow 0110001001101100010000000110
    001101101011011001000110111101100111001011
    100110001101101111001011100111010101101011
    Geoff Guest

  7. #7

    Default Re: break string into an array

    Geoff Berrow wrote: 
    >
    >
    > <fx: smacks forehead> Of course, that's much easier that using
    > $string{n}[/ref]
    LOL, no, thats why I was saying thank you. I found a way on my own but
    yours is quite a bit easier for my purposes.
    Exyle Guest

Similar Threads

  1. hm? array and string?
    By roN in forum PHP Development
    Replies: 2
    Last Post: June 28th, 06:00 AM
  2. string to array
    By Terje in forum PHP Development
    Replies: 2
    Last Post: May 7th, 04:15 PM
  3. Replies: 2
    Last Post: August 16th, 10:26 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139