Results 1 to 3 of 3

Thread: how to echo range of array - e.g. [0:10]

  1. #1
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to echo range of array - e.g. [0:10]

    If I have an array, e.g. a text array, and I want to echo e.g. the first through 10 letter of the array, how do I do that?
    I know this:
    PHP Code:
    $text "bla bla bla bla bla bla bla";
    echo 
    $text[0]; // results in "b"
    echo $text[1]; // results in "l" 
    I tried:
    PHP Code:
    echo $text[0:10]; 
    and all sorts of other combinations - and googled and googled for an answer without succes ...

    I am sure the solution is dead simple ...

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    echo $text[0:10];
    That's the Python solution. The PHP one is:
    Code:
    echo substr($text, 0, 10);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks a bundle!

    Amazing that it can be so hard to find such a simple answer - of course I also searched the PHP manual for e.g. "array range", "array span", looked through the array documentation. If I had searched for substring I would probably have found it. You gotta know the right word ...

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
  •