Re: Strange Substr Behavior

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

 



Ron.Herhuth,Hello!

	You have made a mistake.look at the declaration of substr function:

	string substr(string string, int start, int [length]);
	
	The third parameter means sub-string's length,not the position.Hence,you should do like this:

    <?php
	$searchWord = "acme corporation";
	$searchWord = str_replace(' ','',$searchWord);

	$chars = strlen($searchWord);
	echo "$searchWord<br>";

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

======= 2004-02-28 03:14:36 =======

>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

= = = = = = = = = = = = = = = = = = = =
				 
        Meteorlet Woody
        meteorlet@xxxxxxxxxxx
          2004-02-28


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

  Powered by Linux