How do I change this function from Random to Sequential

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    How do I change this function from Random to Sequential

    Hi,

    I am using this function which is
    used to spin text.

    Currently it looks to me like it is selected an
    element from the $tStringToken array at random.

    These two lines seem to be doing that:
    Code:
    $i = rand(0,$tStringCount); 
    $replace = $tStringToken[$i];
    I would like to change this so that it goes sequentially through
    all of the available elements in the array and then when finished
    starts again with the first element of the array.

    But I am having trouble working it out.

    This is the function I am working on.

    Code:
    function spin($pass){
        $mytext = $pass;
        while(inStr("}",$mytext)){
            $rbracket = strpos($mytext,"}",0);
            $tString = substr($mytext,0,$rbracket);
            $tStringToken = explode("{",$tString);
            $tStringCount = count($tStringToken) - 1;
            $tString = $tStringToken[$tStringCount];
            $tStringToken = explode("|",$tString);
            $tStringCount = count($tStringToken) - 1;
            $i = rand(0,$tStringCount);
            $replace = $tStringToken[$i];
            $tString = "{".$tString."}";
            $mytext = str_replaceFirst($tString,$replace,$mytext);
        }
        return $mytext;
    }
    Any ideas on how I can do this would be very helpful

    Thanks.


    .
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I would like to change this so that it goes sequentially through all of the available elements in the array and then when finished starts again with the first element of the array.
    sounds like a job for the InfiniteIterato r.

    Comment

    • jeddiki
      Contributor
      • Jan 2009
      • 290

      #3
      Thanks Dormilich,

      However, as my code is written in procedural code,
      I was hoping to just change a few lines in my above code to
      do what I need :)

      Is it not so easy then ? ...



      .

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        even in procedural code you can use classes.

        and no, a major change such as from random to sequential usually does not go with changing a few lines of code.

        Comment

        • jeddiki
          Contributor
          • Jan 2009
          • 290

          #5
          OK -

          Well, I'm gonna sit down and write out what each
          of these lines of code means long hand,
          then ...

          I'll figure out a way to make it go through the array sequentially instead of randomly.

          Can't be that hard ... can it ?


          I was hoping that someone with a faster brain than mine, or more experience/expertise could quickly see how this could be done.


          There are 2 other functions which are used:

          Code:
          function inStr($needle, $haystack){
              return @strpos($haystack, $needle) !== false;
          }
          
          function str_replaceFirst($s,$r,$str){
              $l = strlen($str);
              $a = strpos($str,$s);
              $b = $a + strlen($s);
              $temp = substr($str,0,$a) . $r . substr($str,$b,($l-$b));
              return $temp;
          }


          .

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            I was hoping that someone with a faster brain than mine, or more experience/expertise could quickly see how this could be done.
            I find the iterator version very elegant. once it is set up, all you need to do is call ->next() without having to worry where you are in the array.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Currently it looks to me like it is selected an element from the $tStringToken array at random.
              though I doubt $tStringToken is an array ...

              Comment

              Working...