Jump to content

while... what?


DJ Cornbread

Recommended Posts

In the process of testing strtok(), i have noticed a problem with "while"for some reason i cannot set a counter... here is the entire script im using to test it.

<?php$string = "<hr/> you can have this first string<hr/> then string mystring<hr/>  and string yourstringyanus<hr/> and string yourstringyanuses<hr/> and string yourstringyanus,imp<hr/> and string yourstringyanusass<hr/> and string yourstringyanus<hr/> 1234567890abcdefghijklmnopqrstuvwxyz<hr/>!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ<hr/><>{}[]\|/?,.~`<hr/>";$part = explode("<hr/>",$string);$patch = implode("^",$part);$sub = "^";$num = substr_count($patch,$sub);echo $num . "<br/>";$sub = strtok($string, $sub);$i = 1;while ($i < 4 && $sub != false)  {    echo $sub;  $sub = strtok("^");  $i++;  echo $i;  } $t = 1;while ($t <=30)  {  if ($t < 10)  {  echo $t;  $t++;  echo $t;  }  if ($t == 10) {echo "<hr/>break @ $t <hr/>";}  if ($t >= 10 && $t < 20)  {  echo $t;  $t++;  echo $t;  }  if ($t == 20) {echo "<hr/>break @ $t <hr/>";}  if ($t >= 20 && $t <= 30)  {  echo $t;  $t++;  echo $t;  }  if ($t == 30) {echo "<hr/>break @ $t <hr/>";}	  }?>

Notice that $i never echo's while the same use of $t echo's appropriatelyI overused string alot because it was originally string as my "needle" in testing substr_count()what i am attempting is for the string to be divided into groups of 10 much like the counter's readout @ the end of the php file (the 2nd while statement)

Link to comment
Share on other sites

If $i doesn't echo, then $sub is false or something that evaluates to false, like an empty string. That's the only other condition on the while loop. You can use the var_dump function on any variable to see the type and value of the variable, use that to see what $sub is before the first loop starts. I'm confused why you're tokenizing the string instead of just exploding and then looping through the array of pieces. Note that you're also not using $patch once you create it, you're still searching through the original string.

Link to comment
Share on other sites

If $i doesn't echo, then $sub is false or something that evaluates to false, like an empty string. That's the only other condition on the while loop. You can use the var_dump function on any variable to see the type and value of the variable, use that to see what $sub is before the first loop starts. I'm confused why you're tokenizing the string instead of just exploding and then looping through the array of pieces. Note that you're also not using $patch once you create it, you're still searching through the original string.
Thank you!!!! did not have to do what you suggested, but i looked and seen why you mentioned $patch...that was the fail!! i forgot to use it instead of my original (explode and implode were used because <,h,r,/,> would all turn up missing so i used the uncommon ^ character (and will explode/implode them out of user input))this is a copy of the final php as shown on http://test.staticdjs.net/scripttest/subtest.php
<?phpecho "<b>This is the string to be split <br/></b>";$string = "<hr/> you can have this first string<hr/> then string mystring<hr/>  and string yourstringyanus<hr/> and string yourstringyanuses<hr/> and string yourstringyanus,imp<hr/> and string yourstringyanusass<hr/> and string yourstringyanus<hr/> 1234567890abcdefghijklmnopqrstuvwxyz<hr/>!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ<hr/><>{}[]\|/?,.~`<hr/>";$part = explode("<hr/>",$string);$patch = implode("^",$part);$sub = "^";$num = substr_count($patch,$sub);echo $string;echo "<b>Number of times hr/ appears above </b>" . $num . "<br/><br/>";$sub = strtok($patch, $sub);$i = 0;echo "<b>The final string output only allowing 4 segments signified by the hr/</b><br/><hr/>";while ($i < 4 && $sub != false)  {    $p = $sub;  $sub = strtok("^");  echo "$i $p <hr/>";  $i++;  } $t = 0;echo "<br/><br/><b>This is how they can now be split up into groups w/ the counter</b><br/>";while ($t <=30)  {  if ($t < 10)  {  echo "$t, ";  $t++;  }  if ($t == 10) {echo "<hr/>break @ $t <hr/>";}  if ($t >= 10 && $t < 20)  {  echo "$t, ";  $t++;  }  if ($t == 20) {echo "<hr/>break @ $t <hr/>";}  if ($t >= 20 && $t <= 30)  {  echo "$t, ";  $t++;  }  if ($t == 30) {echo "<hr/>break @ $t <hr/>";}	  }?>

i use this string to make sure i do not loose characters not to offend lol

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...