unixmiah unixmiah - 5 months ago 9
PHP Question

storing lines in an array from a db field

How would I go about storing each line in an array? The data below is from a db field in mysql database; I want to loop through the data field, line by line and break them by

\n
line and capture, meaning put each line in an array.

<strong>Apr- May Price: </strong>Adult: $1,999.00 Children: $1,249.00 <br />

<strong>Nov - 15 Dec:</strong> Adult: $2,299.00 Children: $1,725.00 <br />

<strong>Jan - March Price:</strong> Adult: $2,599.00 Children: $2,249.00 <br />

<strong>Jun - Oct; 15-31 Dec Price: </strong>Adult: $2,999.00 Children: $2,249.00 <br />

Answer

It's hard to know what you're asking for, but here's an attempt that may inspire you to stumble upon the solution.

<?php

$html = '<strong>Apr- May Price:  </strong>Adult: $1,999.00 Children: $1,249.00 <br />

<strong>Nov - 15 Dec:</strong> Adult: $2,299.00 Children: $1,725.00 <br />

<strong>Jan - March Price:</strong> Adult: $2,599.00 Children: $2,249.00 <br />

<strong>Jun - Oct; 15-31 Dec Price: </strong>Adult: $2,999.00 Children: $2,249.00 <br />';

$lines = array();

foreach(preg_split("/((\r?\n)|(\r\n?))/", $html) as $line){
    // Only add to array for lines with content
    if(trim($line)!="") $lines[] = $line;
} 

// Print each line as its own array element
print_r($lines);