[HELP] Need Function to Insert a variable after a certain number of words

Discussion in 'PHP' started by phase1, Jul 5, 2010.

  1. #1
    does anyone have any ideas on how to insert a variable after a certain number of words in php?
    i've been trying to find a solution for ages!!
     
    phase1, Jul 5, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    Can you please elaborate, what you mean by "insert a variable after a certain number of words"? It sounds like you only need to give set of loop and conditions, no need for functions. ;)
     
    Rainulf, Jul 5, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Just use:

    PHP code:
    if(strlen($yourparagraph) >= [COLOR="royalblue"]number of words[/COLOR])
    {
       $yourparagraph .= $insertvar;
    }
    Code (markup):
    Please explain your question in more detail if this isn't what you need.
     
    Imozeb, Jul 5, 2010 IP
  4. phase1

    phase1 Active Member

    Messages:
    158
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Essentially, what i would like to do is randomly insert a sentence (which will be held in a variable) into a block of text a certain number of times - so maybe chunk the block of text into sections of 100 words and then randomly insert the sentence into each block of 100 words (and into whatever is left over) - does this make sense?
     
    phase1, Jul 5, 2010 IP
  5. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Its not too difficult to do that really......

    <?php
    
    # $str = the string to populate
    # $word = the word to insert into $str
    # $num = the number of words to insert after
    function strpop($str, $word, $num=10)
      {
      if(str_word_count($str,0,'123456789') <= $num)
        return $str." ".$word;
    
      $tmp = array_keys(str_word_count($str,2, '123456789'));
      for($x=0; $x<sizeof($tmp)-$num; $x+=$num)
        {
        $portion .= substr($str, $tmp[$x], $tmp[($x+$num)]) . $word;
        }
      return $portion;
      }
    $string = "Hello world, this is a test and i am really not writing much taht is interesting to you or me test test";
    $word_to_add = "YOUR ADDED WORD";
    
    $newstr = strpop($string, $word_to_add);
    print $newstr."\n";
    ?>
    PHP:
    OUTPUT: Hello world, this is a test and i am really YOUR ADDED WORDnot writing much taht is interesting to you or me test testYOUR ADDED WORD

    You might want to take it one step further and check if the preceeding (or next) character is a space, and add one if needed..... failing that, add in a space in the concatenation above, and filter out double spaces before returning the string.
     
    lukeg32, Jul 6, 2010 IP
  6. phase1

    phase1 Active Member

    Messages:
    158
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #6
    That solution works well, but do you know if there is a way to randomly insert the added word somewhere within the text "x" number of times (so it's not hardcoded as continually being 10 as in your example)?
     
    phase1, Jul 6, 2010 IP
  7. RobiMac

    RobiMac Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Create and call a function that generates a random number for you prior to calling the function provided above, passing into the $num argument the generated random number.
     
    RobiMac, Jul 6, 2010 IP
  8. phase1

    phase1 Active Member

    Messages:
    158
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #8
    I think i know what you mean. i'm looking to do some math on the size of the string (wordcount) to determine the number of times i loop through the string. For each loop, i would like to generate a new random number (say between 1-42 words) and use this as a reference point for where to insert the word.

    So, for the first loop, we would generate a random number between 1-42 and use this as the first place to insert the word

    For the second loop, we would then add 42 to 42 to give us 84. This would give us the high value of the range. Then we would need to generate a random number between 42 and 84. This random number would be where we would insert the word a second time.

    For the third loop, we would add 42 to 84 to give us 126 to give us the high value of the range. Then we would need to generate a random number between 84 and 126 for where we would insert the word a 3rd time.

    And this would continue on for "X" number of loops

    Anyone know how to code this???
     
    phase1, Jul 6, 2010 IP
  9. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Sorry abuot the delay in replying; yeah, you can just use a random number.

    Actually, a simple change of the above function will do....... the following uses a random number between 2 and 10 (you can pass in a higher number in the 3rd argument).


    <?php
    
    # $str = the string to populate
    # $word = the word to insert into $str
    # $num = the number of words to insert after
    function strpop($str, $word, $num=10)
      {
      if(str_word_count($str,0,'123456789') <= $num)
        return $str." ".$word;
    
      $tmp = array_keys(str_word_count($str,2, '123456789'));
      # initialise random number
      $y=rand(2,$num);
      for($x=0; $x<sizeof($tmp)-$num; $x+=($y=rand(2,$num)))
        {
        $portion .= substr($str, $tmp[$x], $tmp[($x+$y)]) . $word;
        }
      return $portion;
      }
    
    $string = "Hello world, this is a test and i am really not writing much taht is interesting to you or me test test";
    $word_to_add = "YOUR ADDED WORD";
    
    $newstr = strpop($string, $word_to_add);
    print $newstr."\n";
    ?>
    PHP:
    OUTPUT1:Hello world, this is a YOUR ADDED WORDa test and i am really not writingYOUR ADDED WORDreally not writing much taht is interesting to you or me test testYOUR ADDED WORD

    OUTPUT2:Hello world, this is a test and i YOUR ADDED WORDam really not writing much taht is interesting to you or me test testYOUR ADDED WORD
     
    lukeg32, Jul 7, 2010 IP