RE: Strange Substr Behavior

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Here this code will get you what you are looking for.

<?
$searchWord = "acme corporation";

$chars = strlen($searchWord);

echo "$searchWord<br>";

for($i=0;$i<=($chars-3);$i++)
{
#$j = $i * 3;
echo $i . ":" . $j . " ";
echo substr(($searchWord),$i,3) . "<br>";
}
?>

If you want it to look like what you had suggested you would have to remove
the space from the $searchWord variable.

Derrick Hermanson
Programmer/Analyst I
dhermanson@xxxxxxxxx


-----Original Message-----
From: Gerardo Rojas [mailto:grojas@xxxxxxxxxxxxxxxx]
Sent: Friday, February 27, 2004 1:35 PM
To: Ron.Herhuth@xxxxxxxxxxxxxxxxx; php-windows@xxxxxxxxxxxxx
Subject: RE:  Strange Substr Behavior


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@xxxxxxxxxxxxxxxx


-----Original Message-----
From: Ron.Herhuth@xxxxxxxxxxxxxxxxx
[mailto:Ron.Herhuth@xxxxxxxxxxxxxxxxx]
Sent: Friday, February 27, 2004 1:15 PM
To: php-windows@xxxxxxxxxxxxx
Subject:  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

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux