-2

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 />

Thank you all for your help, this is what I was trying to do:

The data:

<strong>Seasonal Price:  </strong>Adult: $1,899.00<br />

<strong>Seasonal Price:  </strong>Adult: $1,499.00<br />

<strong>Seasonal Price:  </strong>Adult: $1,299.00<br />

<strong>Seasonal Price:  </strong>Adult: $1,699.00<br />

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

<strong>Nov - 15 Dec:</strong> Adult: $3,199.00 Children: $2,3990 <br />

<strong>Jan - March Price:</strong> Adult: $3,399.00 Children: $2,549.00 <br />

<strong>Jun - Oct; 15-31 Dec Price: </strong>Adult: $4,799.00 Children: $3,599.00 <br />

Script:

<?php
             //Seasonal Price
             $connection = mysql_connect("localhost","ro**","******#$");

             //select a database
             mysql_select_db("odyssey_prod", $connection);

             $query=mysql_query("select post_content from wp_posts where ID ='$tourid'");
             $row=mysql_fetch_assoc($query);

             //Seasonal Price
             $seasonal_price = $row['post_content'];

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

             // Seasonal Price #1
             preg_match('/(\$[0-9,]+(\.[0-9]{2})?)/', $lines[0], $match);
             $dollar_amount0 = $match[1];

             // Seasonal Price #2
             preg_match('/(\$[0-9,]+(\.[0-9]{2})?)/', $lines[1], $match);
             $dollar_amount1 = $match[1];

             // Seasonal Price #3
             preg_match('/(\$[0-9,]+(\.[0-9]{2})?)/', $lines[2], $match);
             $dollar_amount2 = $match[1];

            // Seasonal Price #4
             preg_match('/(\$[0-9,]+(\.[0-9]{2})?)/', $lines[3], $match);
             $dollar_amount3 = $match[1];
             ?>

             <span class="head"><br/><i class="fa fa-money"></i> Seasonal Price: </span><span class="text-lg lh1em"><?=$dollar_amount0;?></span>

             <span class="head"><br/><i class="fa fa-money"></i> Seasonal Price: </span><span class="text-lg lh1em"><?=$dollar_amount1;?></span>

             <span class="head"><br/><i class="fa fa-money"></i> Seasonal Price: </span><span class="text-lg lh1em"><?=$dollar_amount2;?></span>

             <span class="head"><br/><i class="fa fa-money"></i> Seasonal Price: </span><span class="text-lg lh1em"><?=$dollar_amount3;?></span>


Content from WP/tour

<strong>Seasonal Price:  </strong>Adult: $1,899.00<br />

<strong>Seasonal Price:  </strong>Adult: $1,499.00<br />

<strong>Seasonal Price:  </strong>Adult: $1,299.00<br />

<strong>Seasonal Price:  </strong>Adult: $1,699.00<br />

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

<strong>Nov - 15 Dec:</strong> Adult: $3,199.00 Children: $2,3990 <br />

<strong>Jan - March Price:</strong> Adult: $3,399.00 Children: $2,549.00 <br />

<strong>Jun - Oct; 15-31 Dec Price: </strong>Adult: $4,799.00 Children: $3,599.00 <br />

Result:

enter image description here

unixmiah
  • 3,081
  • 1
  • 12
  • 26

1 Answers1

1

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);
Nick
  • 16,066
  • 3
  • 16
  • 32