RE: [PHP-WIN] Strange Substr Behavior
| From: | Gerardo Rojas | Date: | Fri, 27 Feb 2004 19:35:16 +0000 |
| Subject: | RE: [PHP-WIN] Strange Substr Behavior | ||
| Groups: | php.windows | ||
| Request: | Send a blank email to php-windows+get-23013@lists.php.net to get a copy of this message | ||
The way this is writtien won't work. Because the substr() functions takes 2 arguments. The
first is the start position and the second is the length.
So you are increasing the length or 'j' in the example. It is not a fixed value of 3.
--
Gerardo S. Rojas
mailto: grojas@strategicinc.com
-----Original Message-----
From: Ron.Herhuth@tatumpartners.com
[mailto:Ron.Herhuth@tatumpartners.com]
Sent: Friday, February 27, 2004 1:15 PM
To: php-windows@lists.php.net
Subject: [PHP-WIN] Strange Substr Behavior
I'm trying to cycle through a keyword printing out three character
groupings starting from each letter in the keyword. Here is the script I
am using:
$searchWord = "acme corporation"
$chars = strlen($searchWord);
echo "$searchWord<br>";
for($i=0;$i<=$chars;$i++)
{
$j = $i + 3;
echo $i . ":" . $j . " ";
echo substr(($searchWord),$i,$j) . "<br>";
}
This is the output:
acme corporation
0:3 acm
1:4 cme
2:5 me co
3:6 e corp
4:7 corpor
5:8 corporat
6:9 orporatio
7:10 rporation
8:11 poration
9:12 oration
10:13 ration
11:14 ation
12:15 tion
13:16 ion
14:17 on
15:18 n
16:19
This doesn't make sense to me because it is returning more than 3
characters after the first two iterations. I understand that I have to
modify the script near the end but I'm just trying to get the base concept
working first.
Here is the output I'm looking for:
acm
cme
mec
eco
cor
orp
rpo
por
ora
rat
ati
ion
Thanks,
ROn