Shawn Cooke
Shawn Cooke

Reputation: 967

PHP: parse text from Database line by line

I am storing a text datatype in a database that contains newlines (cr + lf). It is accessed by, for example, $row['mytext']. How can I read $row['mytext'] line by line to do some parsing prior to outputting to html? I know how to do this with a file but have never done it with text from a database.

Upvotes: 1

Views: 510

Answers (1)

chris85
chris85

Reputation: 23880

Once you have $row['mytext'] it is just a string so you can just explode on new lines and then iterate through the array.

Example:

$string = 'line 1
line2';
foreach(explode("\n", $string) as $line) {
    echo $line;
}

Upvotes: 2

Related Questions