Log in

View Full Version : how to echo range of array - e.g. [0:10]



jonas-e
08-17-2007, 03:47 PM
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:

$text = "bla bla bla bla bla bla bla";
echo $text[0]; // results in "b"
echo $text[1]; // results in "l"

I tried:

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 ...

Twey
08-17-2007, 04:24 PM
echo $text[0:10];That's the Python solution. The PHP one is:
echo substr($text, 0, 10);

jonas-e
08-18-2007, 09:20 AM
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 ...