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.

    2 answers 2

    As I understand it, the task is to replace the text, right? So try to find the text you want on the page using "strpos ()" and replay it on ''. This is if I correctly understood the task, if not, then it is necessary to specify ...

    • Added a short description. I think the most understandable. - nick777
    • Duplicate. 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. - nick777
    • I can pay for the help. Call your price. - nick777
    • If you count the words by spaces, then so go? - AseN
    • How else? You can certainly ignore the prepositions. - Oleg

    Asen, instead of wordwrap it is better to use substr_replace, since I need to insert only 1 time. Problem solved. The question can be closed. I found a completely different way, I needed this code to work with CMS Wordpress. There is already a ready solution:

     the_excerpt();