0

I have seen a few questions about this problem, but any answers haven't worked in my case.

I have textarea input in wordpress block, if someone will enter a text like this:

Lorem ipsum
Dolor sit
Amet

I need to get every new line as </li> tag, I was trying to do something like this:

$textAreaData = $PriceListItem[PriceListConfig::ITEMS_LIST_DESCRIPTION];
$textAreaDataList = "<li>" . str_replace("\n", "</li>\n<li>",trim($textAreaData, "\n"));

but it didn't work, can anyone please tell me, what am I doing wrong?

Thank you

  • 1
    Can you clarify "didn't work"? What was the value textAreaDataList after that second line ran? – Don R Apr 23 '21 at 21:29
  • You might want to check this....https://stackoverflow.com/questions/1462720/iterate-over-each-line-in-a-string-in-php – Kevin Gales Apr 23 '21 at 21:43
  • 3
    Does this answer your question? [Iterate over each line in a string in PHP](https://stackoverflow.com/questions/1462720/iterate-over-each-line-in-a-string-in-php) – Jake Apr 24 '21 at 05:07
  • @DonR - After the first line the value was this: "Lorem ipsum Dolor sit Amet" and the second line:
  • Lorem ipsum Dolor sit Amet"
  • – Filip Podhorský Apr 25 '21 at 09:39
  • Something is HTML-encoding the CRLF values in the input before it gets to this code, so there is no newline, there is instead. You can try to track down why that's happening, or you can just replace the CRLF entities ( ) rather than \n (and append a final closing li tag, which you seem to have forgotten) – Don R Apr 25 '21 at 13:42