How to wrap every new line in textarea with li tags? Php

I have a textarea form field where users put a URL separated by a new line. Is it possible to wrap every line from this textarea field using <li> tags?

Therefore, I will need the output from the field so that it is something like this:

<li>some.url.com</li>
<li>some.url.com</li>
<li>some.url.com</li>
<li>some.url.com</li>
<li>some.url.com</li>

Does anyone know who can do this with PHP, please?

+3
source share
3 answers
$textareaData = '<li>'.str_replace("\n","</li>\n<li>",trim($textareaData,"\n")).'</li>';

EDIT

Changed to get rid of all empty lines:

$textareaData = '<li>'.str_replace(array("\r","\n\n","\n"),array('',"\n","</li>\n<li>"),trim($textareaData,"\n\r")).'</li>';
+11
source

What about this type

echo preg_replace('/^(.+)(\s*)$/m', '<li>$1</li>', $text);

The result will be something like this (not nice, but useful):

<li>dsadsa
</li>
<li>dsdsa
</li>
<li>dsadsad
</li>
<li>dsadsadsad
</li>
<li>vcxvxcvxvcxvcx
</li>
<li>fdsfdsfdsfs
</li>
+1
source

, :

$li_text = preg_replace('/^(.+)$/', '<li>$1</li>', $_POST['textarea']);

, ( - ), .

0

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


All Articles