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!!
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.
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.
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?
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.
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)?
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.
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???
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