如何用 li 标签将 textarea 中的每一行换行?PHP
问题描述:
我有一个 textarea 表单字段,用户将在其中放置由新行分隔的 URL.是否可以用 < 来包装这个 textarea 字段中的每一行?li > 标签?
I have a textarea form field where users will put URL's separated by a new line. Would it be possible to wrap each line from this textarea field with < li > tags?
所以我需要字段的输出是这样的:
So I would need the output from the field to be 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>
有谁知道用 PHP 来实现这个吗?
Does anyone know who to achive this with PHP please?
答
$textareaData = '<li>'.str_replace("\n","</li>\n<li>",trim($textareaData,"\n")).'</li>';
编辑
修改为去掉所有空行:
$textareaData = '<li>'.str_replace(array("\r","\n\n","\n"),array('',"\n","</li>\n<li>"),trim($textareaData,"\n\r")).'</li>';