从db字段存储数组中的行

时间:2016-07-04 22:45:38

标签: php

我如何在阵列中存储每一行​​?以下数据来自mysql数据库中的db字段;我希望逐行遍历数据字段并按\n行和捕获它们进行分解,这意味着将每一行放在一个数组中。

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

谢谢大家的帮助,这就是我想要做的事情:

数据:

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

脚本:

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

结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

很难知道你要求的是什么,但这是一种可能会激发你偶然发现解决方案的尝试。

<?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);