We have text, decorated in html tags. For example:

<ul> <li>слова</li> <li>слова</li> <li>слова</li> <li>слова</li> <li>слова</li> <li>слова</li> </ul> <p><span id="more-172"></span><br /> <img src="http://картинка" alt="картинка" title="картинка" width="100" height="100" class="aligncenter size-full wp-image-100" /><br /> слова слова слова слова слова слова слова слова слова слова слова слова слова.</p> 

How to make a selection of the first 20 words? Practices: I think somehow I need to use explode. Sampling by spaces will not work. For example, if there are double spaces or a line break, then an error will already occur (incorrect operation). Clear text is obtained naturally using strip_tags.

    2 answers 2

    Here is the working version:

     $words = preg_split("/[\s]+/", "Текст, который нужно разделить.", 21); $count = 20; for($i=0;$i<$count;$i++){ $a .= ($words[$i].' '); } echo $a; 
       preg_match_all('~<li>(.*?)</li>~is', $html, $m); print_r($m); 
      • 3
        AHAHAH, I also felt about 200 characters? to you [here] [1] [1]: hashcode.ru/questions/81118 - Artem
      • This option will only work for the li tag. The design of the text can be absolutely any. But it does not matter, he found the answer to his question. - nick777