We have a text. For example, such:
Paragraph1: word, word, word, word, word, word.
Paragraph 2: word, word, word, word, word, word.
Paragraph 3: word, word, word, word, word, word.
If we spars the first 20 words and after them put "hello" using this code:
$text = "Текст, который нужно разделить. Здесь у нас много-много абзацев."; $words = preg_split("/[\s\n\r]+/", $text, 21); $count = 20; for($i=0;$i<$count;$i++){ $a .= ($words[$i].' '); } $a .= "привет"; $a = strtr($a, array($text => $a)); echo $a;
As a result, nothing is replaced if there are several paragraphs. How to make, that was replaced, even if several paragraphs?
Briefly: It is necessary to put the word “hello” after the first 20 words, regardless of whether there are line breaks or spaces in the text.