Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    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:
    PHP 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.

    PHP 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.


    .
    Last edited by jeddi; 10-11-2013 at 03:05 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OH

    Can nobody help me ?

    Thanks


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    If it's going to do that on an opened web page in real time you would probably be better off handing the job over to javascript which is able to change DOM elements. To do it sequentially with php you have to have some way of remembering the last value in the sequence, such as a $_SESSION variable. And each time it changes you have to either reload the page or use something like AJAX.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    No - it does not depend on an opened webpage.

    The script loads the text file from a local directory into a string for processing.

    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;
    }
    Hopfully someone with a faster brain than mine, or more experience/expertise can quickly see how this can be done


    .
    Last edited by jeddi; 10-17-2013 at 09:33 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,771
    Thanks
    23
    Thanked 550 Times in 549 Posts
    Thank you for adding the two missing functions. Without them it was unsolvable. You say this is used to spin text. But that is not what I see.

    It is looking for three things in what you give it. The '{', the '}', and the '|'. The first thing it does is to separate what is inclosed between the { and the }. It then looks to see how many '|' are in each section. The variable $tStringCount is the number of '|'s.

    PHP Code:
    $i rand(0,$tStringCount); 
    will give a number between zero and the $tStringCount and chooses which segment will appear in the output.

    Here is a line I feed it
    PHP Code:
    {The} {quick} {bro|wn} {fox} {j|ump|ed} {over} {the} {lazy} {cat
    The words will be given back to you in order, but "bro|wn" will be "bro" or "wn"; a random thing. Same for j|ump|ed. It's j or ump or ed.

    Given that information what do you want?
    And it would be helpful to give us a small input for this thing.

    The output of my input is
    PHP Code:
    The quick bro fox ed over the lazy cat 
    but it's random and could be something else on the next cycle.
    Evolution - The non-random survival of random variants.

  • #6
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OK

    That's brilliant

    So using your input:
    {The} {quick} {bro|wn} {fox} {j|ump|ed} {over} {the} {lazy} {cat}


    $rows = 8;

    How can I make the script output

    1. The quick bro fox j over the lazy cat
    2. The quick wn fox ump over the lazy cat
    3. The quick bro fox ed over the lazy cat
    4. The quick wn fox j over the lazy cat
    5. The quick bro fox ump over the lazy cat
    6. The quick wn fox ed over the lazy cat
    7. The quick bro fox j over the lazy cat
    8. The quick wn fox ump over the lazy cat

    It does 8 rows because $rows = 8;

    i.e. not random but sequential. ?


    Thanks.



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #7
    New Coder
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post
    Try out with the below two functions:

    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;
    }

    Hope this helps.

  • #8
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi priyankagound

    I think your functions are exactly the same ones
    that I posted.

    Did you mis-post ?

    Or did I miss something ??

    Thanks.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #9
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi,

    Any ideas on how to change this script
    from random to sequential ?

    So that using this input:
    {The} {quick} {bro|wn} {fox} {j|ump|ed} {over} {the} {lazy} {cat}

    The script will output:

    1. The quick bro fox j over the lazy cat
    2. The quick wn fox ump over the lazy cat
    3. The quick bro fox ed over the lazy cat
    4. The quick wn fox j over the lazy cat
    5. The quick bro fox ump over the lazy cat
    6. The quick wn fox ed over the lazy cat
    7. The quick bro fox j over the lazy cat
    8. The quick wn fox ump over the lazy cat

    It does 8 rows because $rows = 8;


    The Script that does a random output is:

    PHP Code:
    $rows 8;

    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;
    }

    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;

    Would really appreciate any help - I am stuck

    Thanks.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #10
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,771
    Thanks
    23
    Thanked 550 Times in 549 Posts
    My thoughts on this are, this is a job for recursion and I run at the sound of that word.

    From thinkgeek, "In order to understand recursion, one must first understand recursion."
    Evolution - The non-random survival of random variants.

  • #11
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OH



    Doesn't sound very hopeful



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #12
    New Coder
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    0
    Thanked 2 Times in 2 Posts
    create a variable outside and and increment that as the index of the array

    Code:
    $idx = 0;
    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[$idx++];
           $tString = "{".$tString."}";
           $mytext = str_replaceFirst($tString,$replace,$mytext);
       }
       return $mytext;
    }

  • #13
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OK - thanks for your suggestion

    I will give it a try

    Let you know.



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #14
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi

    I got an error about missing $idx
    so I put $idx inside the function, so I now have:

    PHP 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;
        }

    function 
    sqn_spin($pass){
       
    $mytext $pass;
       
    $idx 0;
       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[$idx++];
           
    $tString "{".$tString."}";
           
    $mytext str_replaceFirst($tString,$replace,$mytext);
       }
       return 
    $mytext;
    }    

    // I use the funtions to generate 100 new files which should
    // sequentially step through the alternative words.

    $Db_arts 100;

    for (
    $art 0$art <= $Db_arts$art++) {
        
        
    // Create files that are named: 1.txt, 2.txt etc.
            
       
    $filename "{$art}.txt";
       
    $filename $path.$filename

       
    $Db_text str_replace("'","`",$Db_text);
       if(
    $Db_rand_cd == 'y') {
        
    $spun_text rand_spin($Db_text);
        }
       else {
        
    $spun_text sqn_spin($Db_text);
                }    
        
    $wordCount explode(" ",$spun_text);
        
    $wordCount count($wordCount);
        
    $spun_text str_replace(chr(13).chr(10),"<br />".chr(13).chr(10),$spun_text);
            
        
    $spun_text wordwrap($spun_text70"\n");     
        
    file_put_contents($filename,$spun_text );
         
        echo 
    "<br>Created File: $filename, Word Count: $wordCount";    
                            
        } 
    // end different articles 
    I am getting these errors:


    Notice: Undefined offset: 4 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 6 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 7 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 8 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 9 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 10 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 11 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 12 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 14 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 15 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 16 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 17 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 18 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 19 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 20 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 21 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 22 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 23 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 24 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 25 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 26 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 27 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 28 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 29 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 30 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 31 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 32 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 33 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 34 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 35 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 36 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 37 in /home/com567b/public_html/auto_spin.php on line 59
    Notice: Undefined offset: 38 in /home/com567b/public_html/auto_spin.php on line 59


    Created File: /home/com567b/spun/0.txt, Word Count: 52

    Then I get the same errors for 1.txt
    and so on for 100 files.

    I don't know why it always starts at offset 4, then misses offset 5, then continues up to
    offset 38.

    Must be a reason ??

    line 59 is : $replace = $tStringToken[$idx++];

    I think the problem is ... I am not checking to see if we have
    run out of the offsets, before incrementing $idx
    ... but I may be wrong

    Is $tStringCount the number of offsets of $tStringToken ??

    How can I add a counting check ?

    Any help very much appreciated


    .
    Last edited by jeddi; 11-01-2013 at 04:33 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #15
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    I was trying to solve this ...
    but I am not sure what this line is doing:

    $replace = $tStringToken[$idx++];

    Why is there a double plus on the end of $idx ?

    Usually I increment a variable at the end of the loop
    is this incrementing during the replace ?

    Or does it mean something else ??

    Thnaks.




    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •