0

I have the contents of a textarea being stored in a PHP string after it is submitted by the user. I am hoping to be able to tweak the formatting of the contents of that string, such that it will be displayable as a list when it is echoed. In other words, I would need to insert UL and /UL at the beginning and end, respectively, and LI and /LI and the beginning and end of each line.

Before I mess with my code, I was wondering if anyone knows if this is this even possible? Are carriage returns sent via textarea submit? Any help/comments would be much appreciated.

[EDIT]

I have defined some variables to give myself all the necessary HTML stuff. The 'repertoire' variable is the original string containing text sent from user input.

$repertoire = ($_POST['repertoire']); 

$list_start = '<UL>';
$list_end = '</UL>';
$list_end = '</UL>';
$list_start_line = '<LI>'; 
$list_end_line = '</LI>';

The following is an example of what would be submitted by the user, and therefore, what would constitute the original $repertoire string:

Luciano Berio - Circles

Mike Svoboda - Piangero la sorte mia

Nicholas von Ritter-Zahony - New Piece

Stefano Gervasoni - Due Poesie Francesi di Rilke

So we would at least need the following:

$repertoire_formatted = substr_replace($list_start, $repertoire, $list_end);

...but I don't know how to substitute <LI> for line breaks; also, I cannot know in advance the length of the string or of each line.

4
  • 1
    yes, that is possible, do mess with your code and show us what you've tried Commented Nov 25, 2018 at 10:27
  • hey, show your string its possible. Commented Nov 25, 2018 at 10:31
  • This is what will be submitted from the textarea. I was hoping that it might be possible to format this without converting it to an array (for complicated reasons). Commented Nov 25, 2018 at 10:36
  • Please edit question and add info to post instead of leaving comment Commented Nov 25, 2018 at 10:42

1 Answer 1

1

You can use regex to selecting every line and wrap it in <li></li>

$html = preg_replace("/([^\n]+)/", "<li>$1</li>", $repertoire);
$html = "<ul>\n$html</ul>";

Check result in demo

Sign up to request clarification or add additional context in comments.

1 Comment

That is a very elegant solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.