Hi,

How would i have a text box where each new line is a bullet list.

<ul>
<li>method line 1</li>
<li>method line 2</li>
</ul>

Can i extract the HTML code tags with the text and insert it into a database with the code and text? So it can be accessed later from the database.

Dani AI

Generated

A short, practical summary that builds on 's point: prefer storing the original textarea value (raw text or a structured array) and generate the <ul> only when you render the page. That keeps the data easy to edit and avoids embedding markup in the database. If you must store markup, store a sanitized cached copy alongside the raw input so edits remain simple.

A robust PHP helper to turn textarea lines into a safe HTML list (handles Windows/Mac/Linux line endings, trims and drops empty lines, escapes for XSS):

function lines_to_ul($text) {
    $lines = preg_split("/\r\n|\r|\n/", (string)$text);
    $lines = array_map('trim', $lines);
    $lines = array_filter($lines, 'strlen');            // remove blank lines
    $safe  = array_map(function($s){                  // escape user text
        return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
    }, $lines);
    $items = array_map(function($s){ return "<li>$s</li>"; }, $safe);
    return "<ul>\n" . implode("\n", $items) . "\n</ul>";
}

Practical tips: insert the raw text into the DB with parameterized queries (PDO or similar). Escape or sanitize only at output; if storing HTML, sanitize on input with a library like HTMLPurifier before saving. For editable lists consider storing a JSON array of lines (json_encode/json_decode) or two columns: raw_text and html_cache. That gives both editability and fast rendering.

Recommended Answers

All 3 Replies

If you want to take plain text from a textarea, and treat each new line as a separate bullet point, you could store the plain text in the database, and then parse the content as follows when retrieving it:

<?php $bullet_points = explode("\n", $plain_text); ?>

<ul>
<?php foreach($bullet_points as $bullet_point): ?>
    <li><?php echo $bullet_point; ?></li>
<?php endforeach; ?>
</ul>

If you're using this on a regular basis, you could wrap the functionality into a function.

Is this what you meant?
R.

Hi,

That is kind of what i want. But can i parse the content before sending it to the database so that it has the tags inside the database?

You can, but it's worth considering whether you will need to edit the list after it's been inserted into the database. If you do, then it would be more difficult to work around the HTML tags.

Anyway, to parse it before inserting:

$list = '<ul><li>' . implode('</li><li>', explode("\n", $plain_text)) . '</li></ul>';

R

commented: good point wrt tags in DB +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.