How to split text by tags using PHP?

I like to break huge text with <li></li> tags after a given number of characters.

For example.

 "biger text1 123 text biger 678 biger text" 

to become

 "<li>biger text1 123 text</li><li>biger 678 biger</li><li> text</li>". 

Is it possible?

+4
source share
2 answers

In one layer, because everyone loves single-line :)

 $s = "biger text1 123 text biger 678 biger text"; $max_line_length = 10; echo join("\n", array_map(function($item) { return '<li>' . $item . '</li>'; }, explode("\0", wordwrap($s, $max_line_length, "\0")))); 

Return:

 <li>biger</li> <li>text1 123</li> <li>text biger</li> <li>678 biger</li> <li>text</li> 
+3
source

try it

 <?php $text = "biger text1 123 text biger 678 biger text"; $newtext = wordwrap($text, 10, "<li>"); echo $newtext; ?> 
+1
source

Source: https://habr.com/ru/post/1414856/


All Articles