Faced the problem of breaking words in the text when inserting a particular line. Ie there is a text, with the help of one function ( substr ) the text is torn in half, put into a variable (by the way, it is used by random to insert in a random place and because of this it’s not what you need). Almost the second part is obtained in this way. Then it all comes together. It’s clear that when dividing a text into two parts, one of the words is accidentally divided, and then the line itself is inserted into this gap. Here is the script itself :

<?php $text = 'наш любимый текст текст текст'; $good = strlen($text); $ran = rand(0,$good); $string = " <b>Скачать чит</b> "; $text1 = substr ($text, 0,$ran); $text2 = substr ($text, $ran); echo $text1 . $string . $text2; ?> 
  • If you are looking for the question "how to divide a line not in the middle of a word," then, obviously, it is necessary to divide it by space. - etki
  • review the best answer - @Deonis provided a more compact version - username

3 answers 3

 <?php mb_internal_encoding("UTF-8"); $text = 'наш любимый текст текст текст'; $string = " <b>Скачать чит</b> "; $mid_pos = mb_strpos($text, ' ', rand(0,mb_strlen($text))); echo mb_substr($text, 0, $mid_pos) , $string , mb_substr($text, $mid_pos); 
  • the best option for me! votes ended - username

It seems that you need to first break the source text into words, and then carry out manipulations with words, not with symbols. For example:

 $text = 'наш любимый текст текст текст'; $string = " <b>Скачать чит</b> "; $words = expolde(' ', $text); $ran = rand(0, count($words)); $text1 = implode(' ', array_slice($words, 0, $ran)); $text2 = implode(' ', array_slice($words, $ran)); echo $text1 . $string . $text2; 

    Well, I will throw out my version, once I wrote

     <?php $text = 'aaaa bbbb cccc dddd eeee'; $string = " <b>xxx</b> "; $mas = explode(" ", $text); $ran = rand(0,count($mas)); array_splice($mas, $ran , 0, $string); $text =implode(" ", $mas); var_dump($text);